如何修复电源查询中的“值不是单个字符串”

Posted

技术标签:

【中文标题】如何修复电源查询中的“值不是单个字符串”【英文标题】:How to fix "The value isn't a single character string" in power query 【发布时间】:2019-08-23 18:30:01 【问题描述】:

我正在尝试使我的电源查询宏与循环数据一起使用,因此我需要用变量替换文件路径和引用。 单元格 37,1 具有 excel 的位置,因此宏可以通过简单的路径编辑在不同的计算机上运行。文本文件是从 SAP 导出的。

我认为问题出在最后的参考文献中。

位置=EH4

.CommandText = Array("SELECT * FROM [EH4]")

Sub GetDataEH4_2()

Dim FromFolder As String
FromFolder = Cells(37, 1).Value

Dim WorkCenter As String
WorkCenter = "EH4.txt"

Dim WC As String
WC = "EH4"

Dim FilePath As String
FilePath = FromFolder & WorkCenter

ActiveWorkbook.Queries(WC).Delete

ActiveWorkbook.Queries.Add Name:=WC, Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(File.Contents(""" & FilePath & """),

...增强查询内容、移动列、删除行、用“”替换单元格值、添加自定义行和确定列类型...

ActiveWorkbook.Worksheets.Add

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:="OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=EH4;Extended Properties=""""" _
    , Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array("SELECT * FROM [EH4]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = WC
    .Refresh BackgroundQuery:=False
End With

使用此代码,错误是“运行时错误'1004':[Expression.Error] 该值不是单字符串。” 我尝试将 .CommandText = Array("SELECT * FROM [EH4]") 更改为 "SELECT * FROM" & WC ) 但给出“无法完成使用数据库驱动程序的操作”

Powerquery 的东西在这里。

"let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(File.Contents(""" & FilePath & """),[Delimiter=""  "", Columns=22, Encoding=65001, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Source,""Column1"", type text, ""Column2"", type text, ""Column3"", type text, ""Column4"", type text, ""Column5"", type text, ""Col" & _
        "umn6"", type text, ""Column7"", type text, ""Column8"", type text, ""Column9"", type text, ""Column10"", type text, ""Column11"", type text, ""Column12"", type text, ""Column13"", type text, ""Column14"", type text, ""Column15"", type text, ""Column16"", type text, ""Column17"", type text, ""Column18"", type text, ""Column19"", type text, " & _
        """Column20"", type text, ""Column21"", type text, ""Column22"", type text)," & Chr(13) & "" & Chr(10) & "    #""Removed Top Rows"" = Table.Skip(#""Changed Type"",7)," & Chr(13) & "" & Chr(10) & "    #""Removed Columns"" = Table.RemoveColumns(#""Removed Top Rows"",""Column1"", ""Column3"", ""Column6"", ""Column7"", ""Column10"", ""Column15"", ""Column17"", ""Column18"", ""Column21"", ""Column4"")," & Chr(13) & "" & Chr(10) & "    #""Reorder" & _
        "ed Columns"" = Table.ReorderColumns(#""Removed Columns"",""Column20"", ""Column19"", ""Column2"", ""Column5"", ""Column8"", ""Column9"", ""Column11"", ""Column12"", ""Column13"", ""Column14"", ""Column16"", ""Column22"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value"" = Table.ReplaceValue(#""Reordered Columns"",""Total"","""",Replacer.ReplaceText,""Column2"")," & Chr(13) & "" & Chr(10) & "    #""Removed Blank R" & _
        "ows"" = Table.SelectRows(#""Replaced Value"", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), """", null)))," & Chr(13) & "" & Chr(10) & "    #""Replaced Value1"" = Table.ReplaceValue(#""Removed Blank Rows"",""User op. status"","""",Replacer.ReplaceText,""Column20"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value2"" = Table.ReplaceValue(#""Replaced Value1"",""Stat"","""",Replacer.ReplaceTex" & _
        "t,""Column19"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value3"" = Table.ReplaceValue(#""Replaced Value2"",""Week"","""",Replacer.ReplaceText,""Column2"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value4"" = Table.ReplaceValue(#""Replaced Value3"",""Work Ctr"","""",Replacer.ReplaceText,""Column5"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value5"" = Table.ReplaceValue(#""Replaced Value4"",""Short text for order"","""",Replacer." & _
        "ReplaceText,""Column5"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value6"" = Table.ReplaceValue(#""Replaced Value5"",""Sales ord."","""",Replacer.ReplaceText,""Column8"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value7"" = Table.ReplaceValue(#""Replaced Value6"",""Op."","""",Replacer.ReplaceText,""Column9"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value8"" = Table.ReplaceValue(#""Replaced Value7"",""Operation text"","""",Repla" & _
        "cer.ReplaceText,""Column11"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value9"" = Table.ReplaceValue(#""Replaced Value8"",""UserFl"","""",Replacer.ReplaceText,""Column12"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value10"" = Table.ReplaceValue(#""Replaced Value9"",""User field (20) 1"","""",Replacer.ReplaceText,""Column13"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value11"" = Table.ReplaceValue(#""Replaced Value10"","" Work""" & _
        ","""",Replacer.ReplaceText,""Column14"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value12"" = Table.ReplaceValue(#""Replaced Value11"",""LatestFin."","""",Replacer.ReplaceText,""Column16"")," & Chr(13) & "" & Chr(10) & "    #""Replaced Value13"" = Table.ReplaceValue(#""Replaced Value12"",""Usr date 2"","""",Replacer.ReplaceText,""Column22"")," & Chr(13) & "" & Chr(10) & "    #""Renamed Columns"" = Table.RenameColumns(#""Replaced Value13" & _
        """,""Column20"", ""System status"", ""Column19"", ""User status"", ""Column2"", ""Week"", ""Column5"", ""Description"", ""Column8"", ""Sales document"", ""Column9"", ""Activity"", ""Column11"", ""Description activity"", ""Column12"", ""Eng category"", ""Column13"", ""Engineer name"", ""Column14"", ""Work hours"", ""Column16"", ""Latest finish d" & _
        "ate"", ""Column22"", ""Est. finished"")," & Chr(13) & "" & Chr(10) & "    #""Added Custom"" = Table.AddColumn(#""Renamed Columns"", ""Comments"", each """")," & Chr(13) & "" & Chr(10) & "    #""Changed Type1"" = Table.TransformColumnTypes(#""Added Custom"",""Latest finish date"", type date, ""Est. finished"", type date, ""Comments"", type text)" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type1"""

【问题讨论】:

如果我理解正确,...[Expression.Error] The value isn't a single-character string. 消息来自 Power Query。我认为您的M/Power Query 代码存在问题(更具体地说,在其中某处您将多字符串传递给需要单个字符的函数)。如果您完整发布 Power Query 代码,可能会有所帮助。 我在问题中添加了电源查询。它直接来自宏,所以格式很糟糕。 谢谢,问题似乎出在Csv.Document 电话中。您在[Delimiter=" ", ...] 中指定的分隔符包含两个空格。您可以尝试替代签名:Csv.Document(File.Contents(...), column names, " ", ExtraValues.Error) - 这将允许您指定多字符分隔符,但还要求您提供有关列以及如何处理额外列的一些信息。或者,您可以将整个文档解析为一列,然后在 2 个空格分隔符上拆分。 【参考方案1】:

在尝试记录 MACRO 以将数据导入电源查询时,可能会出现相同的错误。 在我的情况下,我发现 Delimiter="" "" 更改为 Delimiter=""#(tab)"" 解决了我的问题。

【讨论】:

以上是关于如何修复电源查询中的“值不是单个字符串”的主要内容,如果未能解决你的问题,请参考以下文章

如何修复电源powerd100+%CPU使用率 - macOS

如何修复电源powerd100+%CPU使用率 - macOS

如何修复电源powerd100+%CPU使用率 - macOS

如何修复电源powerd100+%CPU使用率 - macOS

如何修复关于查询中非法字符的 URIException

java - 如何修复Java中本机查询中的无效列名