【问题标题】:Using VBA and Excel Power Query to import user-selected .txt data into Excel使用 VBA 和 Excel Power Query 将用户选择的 .text 数据导入 Excel
【发布时间】:2021-09-12 03:59:49
【问题描述】:

我正在尝试在 Excel for Office 365 中编写一个宏,提示用户选择一个 .txt 文件,然后在删除前 4 行后将文本文件导入现有工作簿中的新工作表文件并将其拆分为列。在录制了一个宏来执行此操作后,我得到了这个,荒谬的第一行:

    ActiveWorkbook.Queries.Add Name:="example sample", Formula:="let" & Chr(13) & "" & Chr(10) & " _
Source = Table.FromColumns({Lines.FromBinary(File.Contents(""C:\Users\fakepath\example _
sample.txt""), null, null, 1252)})," & Chr(13) & "" & Chr(10) & "#""Removed Top Rows"" _ 
= Table.Skip(Source,4)," & Chr(13) & "" & Chr(10) & "#""Split Column by Character _ 
Transition"" = Table.SplitColumn(#""Removed Top Rows"", ""Column1"", Splitter. _ 
SplitTextByCharacterTransition({""0""..""9""}, (c) => not List.Contains({""0""..""9""}, c)), _ 
{""Column1.1"", ""Column1.2"", ""Column1.3"", ""Column1.4"", ""Column1.5"", ""Column1.6"", ""Column1.7"", _
 ""Column1.8"", ""Column1.9""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " _ 
#""Split Column by Character Transition"""

紧随其后

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

我想用用户选择的路径替换代码第一行中的硬编码路径,但我正在努力将其整合到第一行的疯狂中。在其他示例中,我看到人们在该代码之前使用类似(来自本网站上的另一个答案)的内容:

Dim fName As Variant

fName = Application.GetOpenFilename("txtfiles (*.txt), *.txt")
If fName = False Then
MsgBox "No File Was Selected"
Exit Sub
End If

然后用该 fName 替换硬编码路径,但在尝试通过编辑第一行的一部分后:

Source = Table.FromColumns({Lines.FromBinary(File.Contents(""C:\Users\fakepath\example sample.txt""), null, null, 1252)})

Source = Table.FromColumns({Lines.FromBinary(File.Contents(fName), null, null, 1252)})

宏不会运行。 谁能帮我将具有用户选择的给定路径的文件集成到我上面录制的宏中?

【问题讨论】:

  • 你从哪里复制了第一行?它在我的 VBE 中出错。我期待能够用" & somevariablefromfilepicker & " 替换""C:\Users\fakepath\example _ sample.txt""
  • 第一行是直接从我导入文本后“录制宏”按钮创建的宏中复制出来的。

标签: excel vba import powerquery


【解决方案1】:

我没有 Office 365,因此无法对此进行测试:

Dim fd As Office.FileDialog
Dim fName As String
 
Set fd = Application.FileDialog(msoFileDialogFilePicker)
 
With fd
 
    .Filters.Clear
    .Filters.Add "Text Files", "*.txt?", 1
    .Title = "Choose a Text file"
    .AllowMultiSelect = False               'select single file only
    .InitialFileName = "C:\Source Folder"   'change this accordingly
    
    'if a file is selected, assign to fName; exit if not
    If .Show = True Then
        fName = .SelectedItems(1)
    Else
        Exit Sub
    End If
 
End With

'use fName, like so
'Source = Table.FromColumns({Lines.FromBinary(File.Contents(fName), null, null, 1252)})

【讨论】:

  • 抱歉,这不起作用。我在调试中遇到与以前相同的错误:运行时错误 1004 - 应用程序或对象定义错误。它不喜欢我在录制的宏中列出的 with 语句的最后两行: .ListObject.DisplayName = "example_sample" .Refresh BackgroundQuery:=False ... 将它们注释掉也不起作用,我没有数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-08
  • 2021-03-03
相关资源
最近更新 更多