我有一个功能,我想更一般。但我无法将函数的最后一部分作为函数的输入。
我如何使用表作为funktion的输入?我想打破表名并将其用作输入,以便我可以在其他上下文中使用该函数。
Function GetTotalHours("Tablename?" As ??, columnNumber As Integer) As Integer
For Each Row In [Tablename].Rows
getTotalHours = getTotalHours + DateTime.Hour(Row.Columns(columnNumber).Value)
Next
End Function
我能用字符串制作一些怎么样的?
答案 0 :(得分:1)
表格以命名范围的形式存在,您可以使用.Range(name)
直接访问这些。
Function GetTotalHours(tableName As String, columnNumber As Integer) As Integer
Dim r As Range
Dim c As Range
Set r = ActiveSheet.Range(tableName).Columns(columnNumber)
For Each c In r.Cells
GetTotalHours = GetTotalHours + DateTime.Hour(c.Value)
Next
End Function
你也可以传递标题而不是数字:
Function GetTotalHoursByHeading(tableName As String, columnName As String) As Integer
Dim r As Range
Dim c As Range
Set r = ActiveSheet.Range(tableName & "[" & columnName & "]")
For Each c In r.Cells
GetTotalHoursByHeading = GetTotalHoursByHeading + DateTime.Hour(c.Value)
Next
End Function
答案 1 :(得分:1)
如果要避免使用UDF,可以使用此工作表公式
=SUMPRODUCT(HOUR(Table1[Hours]))