创建具有动态范围的表

时间:2016-04-18 15:12:14

标签: vba excel-vba excel

我正在尝试使用VBA创建一个表:

我的代码:

//calculating for cramer method 
$dg=eq1($a,$b,$c);

但是当我运行此代码时,我收到以下错误:

运行时错误'1004':

对象'_Global'的方法'范围'失败

我知道只需说出

就可以使代码更简单
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

但如果我想在for循环中插入此代码,我希望表名和tablerng能够更改。

非常感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

您已获得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