我创建了一个在通过工作簿中的子激活时工作的函数,但是当尝试在工作表中激活它时(使用 =function_name(arguments))
Function calclkg(Optional table_name As String = "lkg_calc_params", Optional ByVal temp As Double = 110) As Double
SetTableValue table_name:=table_name, row_name:="Temp", col_name:="value", value:=temp
calclkg = GetValueFromTable(table_name:=table_name, row_name:="LKG calc", col_name:="value")
End Function
这个函数正在调用这个子,它产生了我得到的错误
Sub SetTableValue(table_name As String, row_name As String, col_name As String, value As Variant, Optional ByVal sheet_name As Variant)
On Error GoTo errline
Dim WS As Worksheet
Dim wb As Workbook
Dim tbl As ListObject
Set WS = Application.Caller.Worksheet.Parent.Worksheets(sheet_name)
Set tbl = WS.ListObjects(table_name)
Dim Rhead As Range
Set Rhead = tbl.HeaderRowRange
col_index = Application.WorksheetFunction.Match(col_name, Rhead, 0)
Dim Rcol As Range
Set Rcol = tbl.ListColumns(1).DataBodyRange
row_index = Application.WorksheetFunction.Match(row_name, Rcol, 0)
' here is the line where I get the error:
tbl.ListColumns(col_index).DataBodyRange.Rows(row_index) = value
Exit Sub
errline:
MsgBox "Error # " & Err & " : " & Error(Err)
Resume Next
End Sub
通过这个太阳调用函数时,我得到了想要的结果:
Sub Calc()
Dim temp As Double
temp = InputBox("temp:")
calclkg temp:=temp
End Sub
直接从我的工作表调用函数时,我无法使用此行更新表格单元格值(出现 1004 错误)
tbl.ListColumns(col_index).DataBodyRange.Rows(row_index) = value
我尝试了各种方法,但都没有奏效。喜欢:
tbl.Range.Item(row_index + 1, col_index) = value
我究竟做错了什么?