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

标签 sql excel vba oledb wildcard

我想通过 VBA 宏将文本文件导入到 excel 过滤器中。当我在 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$1")).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 公式的最后一个技巧:使用宏记录器!

关于带有 OLEDB 的 Sql 字符串在 VBA 中给出错误 1004,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64729285/

相关文章:

sql - 连接数字的存储过程

python - 表名 SQL 语法错误

Excel - 创建一个更复杂的表的简化 "view"(宏?)

excel - 计算日期的小时数

c# - 将公式添加到 Excel 工作表会导致 HRESULT : 0x800A03EC

vba - 以编程方式将图像添加到 PowerPoint 文件

mysql - 将两个计数查询合并为一个

mysql - 我只想要表格中的一行。进行 JOIN 更好还是只进行两次选择调用

xml - 如何提高 VBA 中 XML 解析的速度

arrays - 从数组中删除行