Sql 带有 OLEDB 的字符串在 VBA 中给出错误 1004

Sql string with OLEDB gives error 1004 in VBA

我想将一个文本文件导入 excel,通过 VBA 宏过滤我想要的内容。当我在 sql 字符串中使用 LIKE 运算符时,出现错误 1004。 我试过 *% 作为通配符, ALike 而不是 Like 但没有区别。

test_7.txt

946737295   9CE78280    FF  1   5   FF  FF  FF  FF  FF
946737295   9CE78280    C0  FF  0   0   0   0   FF  FF
946737295   9CE68082    C0  4   0   FF  FF  FF  FF  FF

宏是:

Sub import_txt()

Dim input_path As String
input_path = "C:\test_7.txt"

Dim strSql As Variant
strSql = "SELECT * FROM [test_7]" & _
   " WHERE Column2 Like '*E7*' AND" & _
   " Column4='FF'"
   
    ActiveWorkbook.Queries.Add Name:="test_7", Formula:= _
    "let" & "    Origine = Csv.Document(File.Contents(""" & input_path & """),[Delimiter=""#(tab)"", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None])," & "    #""Modifica tipo"" = Table.TransformColumnTypes(Origine,{{""Column1"", type text}, {""Column2"", type text}, {""Colum" & _
    "n3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & "in" & "    #""Modifica tipo"""
    
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=test_7;Extended Properties=""""" _
    , Destination:=Range("$A")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array(strSql) 'Array("SELECT * FROM [test_7]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "test_7"
    .Refresh BackgroundQuery:=False
End With
End Sub

SQL 语句不适用于 Power Query 提供程序。但是您完全可以通过与您的查询关联的公式实现您想要的。

看这个例子:

Sub readTxt()
Dim Wb As Workbook
Dim Ws As Worksheet
Dim Conn As WorkbookConnection
Dim mFormula As String
Dim query As WorkbookQuery

Set Wb = ActiveWorkbook
Set Ws = Wb.ActiveSheet

mFormula = "let " & _
    "Source = Csv.Document(File.Contents(""C:\Users\Loïc\Desktop\test\test.txt""),[Delimiter="";"", Encoding=65001, QuoteStyle=QuoteStyle.Csv])," & _
    "#""Step1"" = Table.SelectRows(Source, each Text.Contains([Column2], ""E7"") and [Column3] = ""FF"")" & _
    "in #""Step1"""
    
Set query = Wb.Queries.Add("Test text", mFormula)

With Ws.ListObjects.Add(SourceType:=0, Source:= _
     "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & "Test text" & ";Extended Properties=""""", Destination:=Ws.Range("A1"), XlListObjectHasHeaders:=xlYes).QueryTable
     .CommandType = xlCmdSql
     .AdjustColumnWidth = False
     .ListObject.Name = "test"
     .CommandText = "SELECT * FROM [" & "Test text" & "]"
     .Refresh BackgroundQuery:=False
 End With
      
End Sub

您可能想要添加一些错误处理机制(以防文件已经打开等),并在不再需要时删除与文本文件的连接。

您将找到 Microsoft 的 Power Query M 公式语言的文档 here

帮助您使用 VBA 构建 M 公式的最后一个提示:使用宏记录器!