VBA 添加新行以在循环内查询 table
VBA to add new row to query table within loop
我有一个 excel 工作簿,其中包含约 100 个 SQL 连接 table,它们位于单独的 sheet 上,每周刷新一次。我当前的代码是一个循环宏(如下),它遍历每个 sheet 并刷新每个 table.
Sub RefreshLoop()
Dim wks As Worksheet
Dim qt As QueryTable
Dim lo As ListObject
For Each wks In ActiveWorkbook.Worksheets
For Each lo In wks.ListObjects
If lo.SourceType = 3 Then
With lo.QueryTable
.BackgroundQuery = False
.Refresh
End With
End If
Next lo
For Each qt In wks.QueryTables
qt.Refresh BackgroundQuery:=False
Next qt
Next wks
Set qt = Nothing
Set wks = Nothing
End Sub
我想做的是在 table 刷新后,在每个 table 的底部添加一个新行,在第一列中包含当前日期。我试过使用下面的方法,然后在循环中调用它,但我遇到了各种我不太理解的错误。每个 table 都是完全相同的列数,但根据参数的变化具有不同的行。
Dim newrow As ListRow
Set newrow = lo.ListRows.Add(AlwaysInsert:=True)
With newrow
.Range(1) = Date
End With
错误示例;
Run-time error '91':
Object variable or With block variable not set
我正在努力了解我需要使用 Excel 2019
做什么
Sub RefreshLoop()
Dim wks As Worksheet
Dim qt As QueryTable
Dim lo As ListObject
For Each wks In ActiveWorkbook.Worksheets
For Each lo In wks.ListObjects
If lo.SourceType = 3 Then
With lo.QueryTable
.BackgroundQuery = False
.Refresh
End With
End If
If lo.ListRows.Count = 0 Then
lo.ListRows.Add.Range(1) = "No instances of this read code."
End If
lo.ListRows.Add.Range(1) = Date
Next lo
For Each qt In wks.QueryTables
qt.Refresh BackgroundQuery:=False
Next qt
Next wks
Set qt = Nothing
Set wks = Nothing
End Sub
我有一个 excel 工作簿,其中包含约 100 个 SQL 连接 table,它们位于单独的 sheet 上,每周刷新一次。我当前的代码是一个循环宏(如下),它遍历每个 sheet 并刷新每个 table.
Sub RefreshLoop()
Dim wks As Worksheet
Dim qt As QueryTable
Dim lo As ListObject
For Each wks In ActiveWorkbook.Worksheets
For Each lo In wks.ListObjects
If lo.SourceType = 3 Then
With lo.QueryTable
.BackgroundQuery = False
.Refresh
End With
End If
Next lo
For Each qt In wks.QueryTables
qt.Refresh BackgroundQuery:=False
Next qt
Next wks
Set qt = Nothing
Set wks = Nothing
End Sub
我想做的是在 table 刷新后,在每个 table 的底部添加一个新行,在第一列中包含当前日期。我试过使用下面的方法,然后在循环中调用它,但我遇到了各种我不太理解的错误。每个 table 都是完全相同的列数,但根据参数的变化具有不同的行。
Dim newrow As ListRow
Set newrow = lo.ListRows.Add(AlwaysInsert:=True)
With newrow
.Range(1) = Date
End With
错误示例;
Run-time error '91':
Object variable or With block variable not set
我正在努力了解我需要使用 Excel 2019
做什么Sub RefreshLoop()
Dim wks As Worksheet
Dim qt As QueryTable
Dim lo As ListObject
For Each wks In ActiveWorkbook.Worksheets
For Each lo In wks.ListObjects
If lo.SourceType = 3 Then
With lo.QueryTable
.BackgroundQuery = False
.Refresh
End With
End If
If lo.ListRows.Count = 0 Then
lo.ListRows.Add.Range(1) = "No instances of this read code."
End If
lo.ListRows.Add.Range(1) = Date
Next lo
For Each qt In wks.QueryTables
qt.Refresh BackgroundQuery:=False
Next qt
Next wks
Set qt = Nothing
Set wks = Nothing
End Sub