Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 创建具有动态范围的表_Vba_Excel - Fatal编程技术网

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

我正在尝试使用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
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