有没有一种方法可以使用VBA使Excel表格动态化?

有没有一种方法可以使用VBA使Excel表格动态化?,excel,vba,dynamic,Excel,Vba,Dynamic,我正在从外部源导入原始数据,并将其粘贴到现有的原始数据上。我有一个名为“dataTbl”的表,当新数据粘贴到现有数据上时,需要更新该表。这可以通过VBA完成吗 我使用Excel宏记录器记录了以下代码,用新数据更改现有的“dataTbl”。但是,当转换为VBA时,数据似乎不是动态的,无法处理变量。也许还有其他方法可以做到这一点 Option Explicit Dim lastR As Long Dim lastC As Long Dim startCell As Range Dim tblSour

我正在从外部源导入原始数据,并将其粘贴到现有的原始数据上。我有一个名为“dataTbl”的表,当新数据粘贴到现有数据上时,需要更新该表。这可以通过VBA完成吗

我使用Excel宏记录器记录了以下代码,用新数据更改现有的“dataTbl”。但是,当转换为VBA时,数据似乎不是动态的,无法处理变量。也许还有其他方法可以做到这一点

Option Explicit
Dim lastR As Long
Dim lastC As Long
Dim startCell As Range
Dim tblSource As Range
Dim wsSource As Worksheet
Dim wb As Workbook

Sub refresh()
'
'Refresh Excel table with newest Oracle data
'

Set wb = Workbooks("OTR_Dashboard")
Set wsSource = wb.Sheets("RawData")

With wsSource
    Set startCell = Range("A1")
    lastR = wsSource.Cells(Rows.Count, 1).End(xlUp).Row
    lastC = wsSource.Cells(3, Columns.Count).End(xlToLeft).Column
End With

Set tblSource = wsSource.Range(wsSource.Cells(1, 1), wsSource.Cells(lastR, lastC))

Sheets("RawData").Range("A1").Select
    Application.CutCopyMode = False
    'ActiveWorkbook.Name("dataTbl").RefersToR1C1 = tblSource
    ActiveWorkbook.Names("dataTbl").RefersToR1C1 = "=RawData!R1C1:R87671C28"
    Application.CutCopyMode = False

Sheets("DASHBOARD").Select
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveWorkbook.RefreshAll

End Sub

注释掉的行(refrestor1c1=tblSource)给出错误消息“编译错误:参数数目错误或属性分配无效”。它下面的行没有动态数据,工作没有任何问题。

参考
。refresto
而不是
refrestor1c1
结束工作:

Option Explicit
Dim lastR As Long
Dim lastC As Long
Dim startCell As Range
Dim tblSource As Range
Dim wsSource As Worksheet
Dim wb As Workbook

Sub dashRefresh()
'
'Refresh Excel table with newest Oracle data
'

Set wb = Workbooks("OTR_Dashboard")
Set wsSource = wb.Sheets("RawData")

With wsSource
    Set startCell = Range("A1")
    lastR = wsSource.Cells(Rows.Count, 1).End(xlUp).Row
    lastC = wsSource.Cells(3, Columns.Count).End(xlToLeft).Column
End With

Set tblSource = wsSource.Range(wsSource.Cells(1, 1), wsSource.Cells(lastR, lastC))

With ActiveWorkbook.Names("dataTbl")
    .Name = "dataTbl"
    .RefersTo = tblSource
    .Comment = ""
End With
Application.CutCopyMode = False

Sheets("DASHBOARD").Select
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveWorkbook.RefreshAll

End Sub

协助
。referesto
而不是
referestor1c1
最终起作用:

Option Explicit
Dim lastR As Long
Dim lastC As Long
Dim startCell As Range
Dim tblSource As Range
Dim wsSource As Worksheet
Dim wb As Workbook

Sub dashRefresh()
'
'Refresh Excel table with newest Oracle data
'

Set wb = Workbooks("OTR_Dashboard")
Set wsSource = wb.Sheets("RawData")

With wsSource
    Set startCell = Range("A1")
    lastR = wsSource.Cells(Rows.Count, 1).End(xlUp).Row
    lastC = wsSource.Cells(3, Columns.Count).End(xlToLeft).Column
End With

Set tblSource = wsSource.Range(wsSource.Cells(1, 1), wsSource.Cells(lastR, lastC))

With ActiveWorkbook.Names("dataTbl")
    .Name = "dataTbl"
    .RefersTo = tblSource
    .Comment = ""
End With
Application.CutCopyMode = False

Sheets("DASHBOARD").Select
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveWorkbook.RefreshAll

End Sub

尝试使用
ActiveWorkbook.Name(“dataTbl”).referestor1c1=“=”&tblSource.Parent.Name&“!“&tblSource.Address
。这意味着您正在构建一个类似于
”=RawData的字符串!R1C1:R87671C28“
,但它是由变量构建的。请尝试使用
ActiveWorkbook.Name(“dataTbl”)。referestor1C1=“=”&tblSource.Parent.Name&“!“&tblSource.Address
。这意味着您正在构建一个类似于
”=RawData的字符串!R1C1:R87671C28“
,但它是由变量构建的。