编辑:
我调试并发现,即使在引用的工作表中可以看到我前面的所有查询表,Excel仍在任何工作表中计数并找到了0个查询表。
编辑2:
因此,我发现QueryTables
下存在ListObjects
。所以现在,我正尝试创建一个新的数据源连接,如下所示:
tw.Worksheets(2).ListObjects(1).QueryTable.Connection = "OLEDB;" & M1
tw.Worksheets(5).ListObjects(1).QueryTable.Connection = "OLEDB;" & M1
tw.Worksheets(3).ListObjects(1).QueryTable.Connection = "OLEDB;" & M2
tw.Worksheets(6).ListObjects(1).QueryTable.Connection = "OLEDB;" & M2
tw.Worksheets(4).ListObjects(1).QueryTable.Connection = "OLEDB;" & M3
tw.Worksheets(7).ListObjects(1).QueryTable.Connection = "OLEDB;" & M3
原始帖子
我希望使用VBA进一步自动化报告过程。第一步是根据在摘要页面上输入的季度和年份数据刷新所有数据。
我的目标是要做三件事,并且能够更新数据。
下面是我正在尝试使用的代码。我确实有Excel 16,并且我尝试使用excel工作簿连接来更新Macro Workbook。我遇到“下标超出范围”的问题的地方:
tw.Worksheets(2).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M1
Public Sub Refresh_All()
Dim tw As ThisWorkbook: Set tw = ThisWorkbook
Dim qd As Worksheet: Set qd = tw.Worksheets("Quarterly Data")
Dim yr As String: yr = qd.Range("H11").Value2
Dim qrtr As String: qrtr = qd.Range("G11").Value2
Dim fpath As String: fpath = "X:\Dump Report for Loans\" & yr
Dim ldr As String: ldr = "\Loan Dump Report (000.Original).xlsx"
Dim M1 As String
Dim M2 As String
Dim M3 As String
If qrtr = "Q1" Then
M1 = fpath & "\02-01-" & yr & ldr
M2 = fpath & "\03-01-" & yr & ldr
M3 = fpath & "\04-01-" & yr & ldr
tw.Worksheets(2).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M1
tw.Worksheets(2).QueryTables(1).Refresh
tw.Worksheets(3).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M2
tw.Worksheets(3).QueryTables(1).Refresh
tw.Worksheets(4).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M3
tw.Worksheets(4).QueryTables(1).Refresh
ElseIf qrtr = "Q2" Then
M1 = fpath & "\05-01-" & yr & ldr
M2 = fpath & "\06-01-" & yr & ldr
M3 = fpath & "\07-01-" & yr & ldr
tw.Worksheets(2).QueryTables(1).Item Connection:=M1
tw.Worksheets(2).QueryTables(1).Refresh
tw.Worksheets(3).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M2
tw.Worksheets(3).QueryTables(1).Refresh
tw.Worksheets(4).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M3
tw.Worksheets(4).QueryTables(1).Refresh
ElseIf qrtr = "Q3" Then
M1 = fpath & "\08-01-" & yr & ldr
M2 = fpath & "\09-01-" & yr & ldr
M3 = fpath & "\010-01-" & yr & ldr
tw.Worksheets(2).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M1
tw.Worksheets(2).QueryTables(1).Refresh
tw.Worksheets(3).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M2
tw.Worksheets(3).QueryTables(1).Refresh
tw.Worksheets(4).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M3
tw.Worksheets(4).QueryTables(1).Refresh
ElseIf qrtr = "Q4" Then
M1 = fpath & "\11-01-" & yr & ldr
M2 = fpath & "\12-01-" & yr & ldr
M3 = fpath & "\01-01-" & yr + 1 & ldr
tw.Worksheets(2).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M1
tw.Worksheets(2).QueryTables(1).Refresh
tw.Worksheets(3).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M2
tw.Worksheets(3).QueryTables(1).Refresh
tw.Worksheets(4).QueryTables(1).Item Connection:="EXCEL.Worksheets;" & M3
tw.Worksheets(4).QueryTables(1).Refresh
Else:
MsgBox "ERROR!! NO QUARTER ENTERED!!"
End If
ThisWorkbook.RefreshAll
End Sub
在此先感谢您的帮助!
答案 0 :(得分:0)
在与@chillin进行了一些外部讨论之后,我们提出了一个使用M/Power Query
使其更具动态性的解决方案。下面是运行良好的代码。再次感谢@chillin。
let
GetNamedRangeInCurrentWorkbook = (nameOfRange as text) as any => Excel.CurrentWorkbook(){[Name=nameOfRange]}[Content][Column1]{0},
quarterFromSheet = Text.From(GetNamedRangeInCurrentWorkbook("quarter")),
yearFromSheet = Text.From(GetNamedRangeInCurrentWorkbook("year")),
quarterNumber = try Number.From(Text.End(quarterFromSheet, 1)) otherwise error "Could not convert 'quarter' to a number",
filename = "Loan Dump Report (000.Original).xlsx",
startOfFinancialYear = #date(Number.From(yearFromSheet), 2, 1),
MonthInFilePath = (startDate as date, quarter as number, month as number) as date => Date.AddMonths(startDate, ((quarter-1) * 3) + month - 1),
DetermineFilePath = (month as number) as text =>
let
targetMonth = MonthInFilePath(startOfFinancialYear, quarterNumber, month), folderPath = "X:\Dump Report for Loans\" & Date.ToText(targetMonth, "yyyy") & "\", subFolder = Date.ToText(targetMonth, "MM-dd-yyyy"), fullFilePath = folderPath & subFolder & "\" & filename
in
fullFilePath
in
DetermineFilePath