我无法弄清楚为什么这句话不起作用。
Rng = Worksheets(sheetName).ListObjects(table).Range.Select
我有一张带有一个按钮的工作表“sheetX”,该按钮在全局工作区“Thisworkbook”中调用子进程“export_json”。我希望“Thisworkbook”中的子进程在“A2”的“sheetX”上引用表格范围,但它给出错误“应用程序定义的或对象定义的错误”。我不想使用Application.Goto
为什么?我忽略了一些基本的东西
Public Sub CommandButton1_Click()
sheet = ActiveSheet.Name
Call ThisWorkbook.export_json(sheet)
End Sub
Public Sub export_json(sheetName)
table = ThisWorkbook.get_table(Worksheets(sheetName).Range("A2"))
Rng = Worksheets(sheetName).ListObjects(table).Range.Select
Rng = Selection.Address
table的类型为string,sheet是string类型的正确表单名称,因此不是问题。
答案 0 :(得分:0)
您的语法设置看起来有些偏差,当您尝试在函数Rng
中声明export_json()
时,您应该将其作为字符串传递。
Public Sub CommandButton1_Click()
Dim sheetX As String
sheetX = ActiveSheet.Name
Call export_json("sheetX")
End Sub
Private Function export_json(sheetName As String)
table = ThisWorkbook.get_table(Worksheets(sheetName).Range("A2"))
Worksheets(sheetName).ListObjects(table).Range.Select
End Function
答案 1 :(得分:0)
尝试以下代码,无需使用Select
。
另外,我不清楚为什么要将Function
的代码保存在ThisWorkbook
模块中,而不是常规模块。
Public Sub CommandButton1_Click()
Dim TableRangeString As String
TableRangeString = ThisWorkbook.export_json(ActiveSheet.Name)
' for debug
MsgBox TableRangeString
End Sub
Public Function export_json(sheetName) As String
' use the function to return the Range.Address by changing it to return a String
Dim Rng As Range
Set Rng = Worksheets(sheetName).Range("A2").ListObject.Range
export_json = Rng.Address
End Function