错误1004提供的路径必须是有效的绝对路径

时间:2018-04-01 20:40:48

标签: excel excel-vba excel-2010 vba

我正在尝试将多个csv文件导入excel。我运行了宏录制器并获得了我拥有的大部分代码。当我有实际的文件路径时,代码工作,但一旦我用变量替换实际路径。我尝试过6或7种组合但没有运气。该错误是由.Refresh BackgroundQuery:= False行引起的。但是当我改变线路时发生了错误 Source = Csv.Document(File.Contents(“”C:\ Documents and Settings \ Parents \ Downloads \ DBO.csv“”),[Delimiter =“”,“”,Columns = 6,

Source = Csv.Document(File.Contents(Chr(34)& Chr(39)& strFullPath& Chr(39)& Chr(34)),[Delimiter =“”,“”,  我的代码如下:

                        Public Sub ImportFiles()

                            Dim xStrPath As String
                            Dim xFile As String
                            Dim xFiles As New Collection
                            Dim I As Long
                            Dim lastRowSrc As Long
                            Dim lastRowDest As Long
                            Dim strFileName As String
                            Dim destWB As Workbook
                            Dim destWS As Worksheet
                            Dim srcWB As Workbook
                            Dim strSrcFile As String
                            Dim strDestFile As Worksheet
                            Dim strColRow As String
                            Dim strFullPath As String


                                xStrPath = "C:\Documents and Settings\Parents\Downloads\"
                                If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\"
                                xFile = Dir(xStrPath & "*.csv")
                                strFileName = Left(xFile, 3)
                                lastRowDest = Sheets(strFileName).Range("A" & Rows.Count).End(xlUp).Row + 1
                                strColRow = "$A$" & lastRowDest

                            Do While xFile <> ""
                                xFiles.Add xFile, xFile
                                xFile = Dir()
                            Loop
                                Set destWB = Workbooks.Add

                            If xFiles.Count > 0 Then
                                For I = 1 To xFiles.Count
                                    Set srcWB = Workbooks.Open(xStrPath & xFiles.Item(I))

                                    Stop
                                    strSrcFile = xFiles.Item(I)
                                    strFullPath = xStrPath & xFiles.Item(I)
                                    strFileName = Left(xFiles.Item(I), 3)

                                    destWB.Activate


                                'Run-time error 1004
                                'the import Chr matches no exports. Did you miss a module reference?
                                 destWB.Queries.Add Name:=strFileName, Formula:= _
                                        "let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(File.Contents(Chr(34) & Chr(39) & strFullPath & Chr(39) & Chr(34)),[Delimiter="","", Columns=6, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""date"", type date}, {""close" & _
                                        """, type number}, {""volume"", Int64.Type}, {""open"", type number}, {""high"", type number}, {""low"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""


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

                                    Sheets("Sheet1").Name = strFileName
                                    With ActiveSheet.ListObjects(strFileName)
                                        .ShowAutoFilterDropDown = False
                                        .ShowTableStyleRowStripes = False
                                        .TableStyle = ""
                                        .Sort.SortFields.Clear
                                        .Sort.SortFields.Add Key:= _
                                        Range("DBO[date]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
                                        :=xlSortNormal
                                    End With

                                    With ActiveWorkbook.Worksheets(strFileName).ListObjects(strFileName).Sort
                                        .MatchCase = False
                                        .Orientation = xlTopToBottom
                                        .SortMethod = xlPinYin
                                        .Apply
                                    End With


                                    Set destWS = destWB.Worksheets(I)
                                    Call FixSheet(destWB, destWS)
                                Next
                            End If
                        End Sub

0 个答案:

没有答案