如果不知道客户 ID,我需要浏览一份工作报告文件列表并搜索客户 ID 号或公司名称的通配符部分选择。
我清理了查询以删除大部分多余的不必要字段,然后停在那里。我还打算将 2 个不同的查询合并到 1 个程序语句中,但它与我抗争,我停在那里。
为临时查询放置制作一个名为“输出”的工作表。它只是复制数据结果而不是标题,因为我将多个结果串在一起。您当然需要记录宏和数据/获取数据/从文件/从工作簿,打开工作簿,转换数据,选择要返回的列,然后在列上输入搜索参数,然后关闭并返回到您的电子表格,最后停止宏以获取您自己的查询。
Sub XLDataScan()
' Send File path and Name of XL file, Specific data, OR Contains data to search for.
ExternalXLScan "PATH/FILENAME", "SPECIFIC_Data", "CONTAINS_Data"
End Sub
Sub ExternalXLScan (sPath As String, sSubID As String, sOrg As String)
Dim DoSearch As String
Sheets("Output").Select
' The 2 data needed for either query is "sPath", which is the file to be checked, and the "sSubID" OR "sOrg".
' SPECIFIC or PARTIAL
If sSubID <> "" Then
DoSearch = "([Subscriber ID] = " & sSubID
ActiveWorkbook.Queries.Add Name:="Add-On Pull", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""" & sPath & """), null, true)," & Chr(13) & "" & Chr(10) & " #""Add-On Pull_Sheet"" = Source{[Item=""Add-On Pull"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(#""Add-On Pull_Sheet"", [PromoteAllScal" & _
"ars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Pull Date"", type date}, {""Mail Date"", type date}, {""Job Line"", type any}, {""Account Name"", type text}, {""Account State"", type text}, {""Last Name"", type text}, {""Suffix"", type any}, {""First Name"", type text}, {""Middle Name"", type text}, {""Subscriber ID"", Int64" & _
".Type}, {""CertificateDeductibleperCoveredPerson"", type any}, {""CertificateDeductibleperFamily"", type any}})," & Chr(13) & "" & Chr(10) & " #""Removed Other Columns"" = Table.SelectColumns(#""Changed Type"",{""Mail Date"", ""Account Name"", ""Last Name"", ""First Name"", ""Subscriber ID""})," & Chr(13) & "" & Chr(10) & " #""Filtered Rows"" = Table.SelectRows(#""Removed " & _
"Other Columns"", each " & DoSearch & "))" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Filtered Rows"""
End If
If sOrg <> "" Then
' Text.Contains([Account Name], ""Series"
Debug.Print "sOrg: " & sOrg
DoSearch = "Text.Contains([Account Name], """ & sOrg '"([Subscriber ID] = " & sOrg
ActiveWorkbook.Queries.Add Name:="Add-On Pull", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""" & sPath & """), null, true)," & Chr(13) & "" & Chr(10) & " #""Add-On Pull_Sheet"" = Source{[Item=""Add-On Pull"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(#""Add-On Pull_Sheet"", [PromoteAllScala" & _
"rs=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Pull Date"", type date}, {""Mail Date"", type date}, {""Job Line"", type any}, {""Account Name"", type text}, {""Account State"", type text}, {""Last Name"", type text}, {""Suffix"", type any}, {""First Name"", type text}, {""Middle Name"", type text}, {""Subscriber ID"", Int64." & _
"Type}, {""CertificateDeductibleperFamily"", Int64.Type}})," & Chr(13) & "" & Chr(10) & " #""Removed Other Columns"" = Table.SelectColumns(#""Changed Type"",{""Pull Date"", ""Account Name"", ""Last Name"", ""First Name"", ""Subscriber ID""})," & Chr(13) & "" & Chr(10) & " #""Filtered Rows"" = Table.SelectRows(#""Re" & _
"moved Other Columns"", each " & DoSearch & """))" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Filtered Rows"""
End If
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Add-On Pull"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Add-On Pull]")
' .RowNumbers = True
.ListObject.DisplayName = "Add_On_Pull"
.Refresh BackgroundQuery:=False
End With
' Remove Query and Connection
KillQueries
'If data, copy it over
If Range("A2") <> "" Then
' Just copy data found, not including header
Dim AllFound As Integer
AllFound = Worksheets("Output").Range("A" & Rows.Count).End(xlUp).Row
Workbooks("Transconnect_Production.xlsm").Worksheets("Output").Range("A2:E" & AllFound).Copy _
Destination:=Workbooks("Transconnect_Production.xlsm").Worksheets("Find Mail Date").Range("B" & RowPlace + 1)
Range("Add_On_Pull[#All]").Delete
Sheets("Sheet1").Select
End Sub
Sub KillQueries()
Dim xConnect As Object
Dim cn As WorkbookConnection
Dim qr As WorkbookQuery
On Error Resume Next
For Each cn In ThisWorkbook.Connections
cn.Delete
Next
For Each qr In ThisWorkbook.Queries
qr.Delete
Next
End Sub