Vba 创建具有动态范围的表
我正在尝试使用VBA创建表: 我的代码:Vba 创建具有动态范围的表,vba,excel,Vba,Excel,我正在尝试使用VBA创建表: 我的代码: Sub MakeTablesDynamic() Application.ScreenUpdating = False 'This hides the visual process and speeds up 'the execution Dim tablerng As Range Dim tablename As String Dim FirstTableRow As Integer
Sub MakeTablesDynamic()
Application.ScreenUpdating = False 'This hides the visual process and speeds up
'the execution
Dim tablerng As Range
Dim tablename As String
Dim FirstTableRow As Integer
Dim LastTableRow As Integer
tablename = Right(Cells(7, 1).Value, 25)
FirstTableRow = 12
LastTableRow = 18
Set tablerng = ActiveSheet.Range(Cells(FirstTableRow, 2), Cells(LastTableRow, LastColumn))
ActiveSheet.ListObjects.Add(xlSrcRange, Range(tablerng), , xlYes).Name = tablename
但是,当我运行此代码时,会出现以下错误:
运行时错误“1004”:
对象“\u Global”的方法“Range”失败
我知道我可以通过简单地说
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$B$12:$G$18"), , xlYes).Name = tablename
但如果我想在for循环中插入这段代码,我希望表名和表名能够更改
非常感谢您提供的任何帮助或建议。您已经获得了Range(tablerng)
。您要求它创建一个范围的范围。正如您已经声明的,tablerng
作为一个范围,您只需使用tablerng
Sub MakeTablesDynamic()
Application.ScreenUpdating = False 'This hides the visual process and speeds up
'the execution
Dim tablerng As Range
Dim tablename As String
Dim FirstTableRow As Integer
Dim LastTableRow As Integer
tablename = Right(Cells(7, 1).Value, 25)
FirstTableRow = 12
LastTableRow = 18
Set tablerng = ActiveSheet.Range(Cells(FirstTableRow, 2), Cells(LastTableRow, LastColumn))
ActiveSheet.ListObjects.Add(xlSrcRange, tablerng, , xlYes).Name = tablename