2014-12-04 62 views
1

我試圖創建一個用戶定義的函數來計算在列A中使用的細胞數量,然後通過10自定義函數計算所用的細胞數在一列在Excel 2010

多個這種不幸的是, UDF是一個完全不同於我通常放在一起的宏的編碼野獸。

Function UsedCells() As Long 
n = Worksheets("Marginal").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count 
y = n * 10 
End Function 

這不起作用 - 任何人都提供幫助嗎?謝謝!

編輯編輯 此代碼結束了工作基礎上的聖誕代碼:

Function UsedCells(Col As String) As Long 
n = Range("A" & Rows.Count).End(xlUp).Row 
UsedCells = n * 10 
End Function 

回答

2

的公式變量。因此請設置功能(UsedCells)的名字=你的結果:

UsedCells = n * 10

編輯:請注意這一點,如果你有更多的細胞在山坳A.爲了使動態計算將不重新計算使用:

Function UsedCells(Col As String) As Long 
n = Worksheets("Marginal").Range(Col & ":" & Col).Cells.SpecialCells(xlCellTypeConstants).Count 
UsedCells = n * 10 
End Function 

然後在表格中的任何給定單元格中放置forumla =UsedCells("x")其中x是要檢查的列的字母。

工作液:

Function UsedCells(Col As String) As Long 
UsedCells = Sheets("Marginal").Range(Col & Rows.Count).End(xlUp).Row * 10 
End Function 
+0

對不起通過你的意思困惑?你的意思是什麼 – user3682157 2014-12-04 21:33:34

+0

函數'UsedCells'的名字是變量。我正在添加更多的細節以使它對您更有用。 – Chrismas007 2014-12-04 21:34:20

+0

添加了替代方法。增加了更多細節。 – Chrismas007 2014-12-04 21:38:01