用于计算Excel 2010中列中已使用单元格数的自定义函数

时间:2014-12-04 21:26:55

标签: excel vba excel-vba

我试图创建一个用户定义的函数来计算A列中已用单元格的数量,然后将其乘以10。

不幸的是,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

1 个答案:

答案 0 :(得分:2)

公式是变量。因此,将函数名称(UsedCells)=设置为结果:

UsedCells = n * 10

编辑:请注意,如果在col 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
相关问题