2014-12-04 67 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