[英]Excel VBA get table range on unactive worksheet
I cannot figure out why this statement does not work. 我不知道为什么这个说法行不通。
Rng = Worksheets(sheetName).ListObjects(table).Range.Select
I have a sheet "sheetX" with a button that invokes a subprocess "export_json" in the global workspace "Thisworkbook". 我有一个带有一个按钮的工作表“ sheetX”,该按钮在全局工作区“ Thisworkbook”中调用子过程“ export_json”。 I want the subprocess in "Thisworkbook" to reference a table range on "sheetX" at "A2" but it gives an error "Application-defined or Object-defined error".
我希望“ Thisworkbook”中的子进程引用“ A2”处“ sheetX”上的表范围,但它给出错误“应用程序定义或对象定义的错误”。 I do not want to use Application.Goto
我不想使用Application.Goto
Why is that? 这是为什么? I'm overlooking something basic
我忽略了一些基本的东西
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 is of type string and sheet is the correct sheet name of type string so that is not the problem. table是字符串类型,而sheet是字符串类型的正确工作表名称,因此这不是问题。
Your syntaxing looks a little off, when your trying declare Rng
in the function export_json()
you should pass it as a string. 您的语法看起来有些
Rng
,当您尝试在函数export_json()
声明Rng
,应将其作为字符串传递。
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
Try the code below, there's no need to use Select
. 尝试下面的代码,无需使用
Select
。
Also, it is not clear to me why you keep the code of your Function
in ThisWorkbook
module, instead of a regular module. 另外,我不清楚为什么将
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.