自定义函数来计算Excel 2010中列中的已用单元的数量

我正在尝试创build一个用户定义函数来计算列A中使用的单元格的数量,然后再乘以10。

不幸的是,UDF是一个完全不同于我通常放在一起的macros的编码野兽。

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 

公式是variables。 所以设置你的函数的名称(UsedCells)= =你的结果:

UsedCells = n * 10

编辑:请注意,这将不会重新计算,如果你使用更多的单元格列A.要使其dynamic计算使用:

 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是要检查的列的字母。

工作scheme:

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