[英]excel UDF doesn't work when used in sheet
我創建了一個通過工作簿中的子目錄激活的功能,但是嘗試在工作表中激活它時(使用= 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
我究竟做錯了什么?
UDF不能以任何方式更改工作表的結構,例如復制單元格或更改字體。 UDF可以調用其他函數和子例程,但是它們與UDF具有相同的約束。
本質上,UDF會接受參數並將值返回到公式所在的單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.