将硬编码文件更改为用户提示文件时出错

时间:2017-12-04 14:14:21

标签: excel vba excel-vba

您好,我正在尝试将CS​​V文件中的数据导入Excel工作表。我录了宏。但是宏使用硬编码文件。代码在

之下
Sub ImportData()
    ActiveWorkbook.Queries.Add Name:="XX", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(File.Contents(""N:\Operations\Workshop\04 Turbine Testing\01 Engine Test Data\SN140\Build01\Data\Raw\OPC data\SN140-01-07 250kW test.csv""),[Delimiter="","", Columns=104, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.Tra" & _
        "nsformColumnTypes(#""Promoted Headers"",{{""Timestamp"", type datetime}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.Engine_Shaft_Power"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.HT80"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PT80"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99" & _
        "_Signals.TE80"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]SE210A"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]SE210B"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]SE210C"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]SY210"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.Engine_Inlet_Mass_Flow"", type number}, {""[I00001" & _
        "9_Testcell_Heng_Rev_0_12]Program:TB99_Signals.FT912"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PDT110"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PT925"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PT931"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PT980A" & _
        """, type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PT980B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE130A"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE130B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE970"", type number}, {""[I000019_Testcell_Heng_Rev_0" & _
        "_12]Program:TB99_Signals.TE971"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE972"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE973"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]PT364A"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]PT364B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]PT375"", " & _
        "type number}, {""[I000019_Testcell_Heng_Rev_0_12]PT376"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]PY364"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE141A"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE141B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE142A"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE142B"", type number}, {" & _
        """[I000019_Testcell_Heng_Rev_0_12]TE143A"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE143B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE144A"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE144B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]ZT370"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]FV370"", Int64.Type}, {""[I000019_Testce" & _
        "ll_Heng_Rev_0_12]FV370B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TT371"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]PT360"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]ZT361"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]FV361"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]FV361B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]ZT" & _
        "366"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]FV366"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]FV366B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.FT933A"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE940"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE941"", type nu" & _
        "mber}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE942"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE943"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE944"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE945"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB" & _
        "99_Signals.TE946"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE947"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE948"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE949"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE950"", type number}, {""[I000019_Testce" & _
        "ll_Heng_Rev_0_12]Program:TB99_Signals.PDT953"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PDT954"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PDT955"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PDT956"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE161"", type number}, {""[I000" & _
        "019_Testcell_Heng_Rev_0_12]TE162"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE163"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE164"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE165"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE166"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TY160"", type number}, {""[I000019_Testcell_Heng_Re" & _
        "v_0_12]PT160"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.Turbine_Entry_Temperature"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]VY210"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]VY215"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]VY220"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]PT240"", type number}, {""[I00" & _
        "0019_Testcell_Heng_Rev_0_12]TE231"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]PT231"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE233"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]PT381"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:Flame_Monitoring.BT_DERV01.Out"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signa" & _
        "ls.PT130A"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.PT130B"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE951"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE952"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE110A"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE110B""," & _
        " type number}, {""[I000019_Testcell_Heng_Rev_0_12]TE362"", type number}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB88_Signals.VY218"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB88_Signals.VY219"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB88_Signals.VY221"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB88_Signals.VY222" & _
        """, Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB88_Signals.VY223"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB88_Signals.VY224"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]FT234"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]FT235"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:Combustor_Vibration_Signals.VY141A"", " & _
        "Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:Combustor_Vibration_Signals.VY141B"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:Combustor_Vibration_Signals.VY142A"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:Combustor_Vibration_Signals.VY142B"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB88_Signals.VY2161"", Int64.T" & _
        "ype}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB88_Signals.VY2171"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.Gas_Fuel_Mass_Flow"", Int64.Type}, {""[I000019_Testcell_Heng_Rev_0_12]Program:TB99_Signals.TE929"", type number}, {"""", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    ActiveWorkbook.Worksheets.Add


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

    Range("A1").Select

End Sub

我想将代码中的硬编码文件替换为用户提示文件,因此将以下代码添加到上面

Dim path As String

With Application.FileDialog(msoFileDialogOpen)
    .Show
    If .SelectedItems.Count = 1 Then
        path = .SelectedItems(1)
    End If
End With

ActiveWorkbook.Queries.Add Name:="XX", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(File.Contents (path).... remaining code as it was

这不起作用,它在行中给出错误         .Refresh BackgroundQuery:=False

PS:我对宏非常陌生......所以请帮助我。谢谢!

2 个答案:

答案 0 :(得分:0)

没有足够的代表要求澄清所以这里......

当我想获得用户选择的文件时,我使用这样的东西。在这种情况下,我将修改上下文以引用CSV:

    Dim importFile as String

    'Displays an open file dialog box for selecting the target file.
    importFile = Application.GetOpenFilename( _
    FileFilter:="CSV Files, *.csv; Title:="Select a CSV file")

    'Handles no sheet selection.
    If importFile = "False" Then
        MsgBox "No File Selected!"
        Exit Sub
    End If

从那里你可以使用importFile作为路径。试试这个

    Source = Csv.Document(File.Contents(importFile)...

答案 1 :(得分:0)

这应该完成它。

"let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(File.Contents(" & """" & importFile & """" & "),[Delimiter=""#(tab)"", ...