簡體   English   中英

Vba 將 CSV 文件導入 Excel

[英]Vba Import CSV files to Excel

我正在嘗試使用 VBA 代碼將 Csv 文件導入到 excel,我希望它可以與所有 Csv 文件一起使用,但它沒有。 這是我的代碼:

Sub Input_CSV()

Dim Wb As String
Dim Arr

    Set Wb = GetObject(Application.GetOpenFilename("csv file,*.csv", , "please choose a csv file", , False))

    Dim blnImportData As Boolean
    blnImportData = ImportCSV(Wb, "Sheet1", "A1")
    If blnImportData Then MsgBox "Import CSV process complete", vbInformation, ThisWorkbook.Name _
    Else MsgBox "Import CSV process failed", vbCritical, ThisWorkbook.Name

End Sub

這是功能代碼

Function ImportCSV(ByVal Filename As String, _
                   ByVal Worksheet As String, _
                   ByVal StartCell As String) As Boolean
 On Error GoTo Catch

    Dim strConnectionName As String
    strConnectionName = "TEXT;" + Filename

    With Worksheets(Worksheet).QueryTables.Add(Connection:=strConnectionName, _
                                               Destination:=Worksheets(Worksheet).Range(StartCell))
        .Name = Filename
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True 'False
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True 'False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .Refresh BackgroundQuery:=False
    End With

        ImportCSV = True
        Exit Function

Catch:
        ImportCSV = False

End Function

我怎樣才能糾正它? 有人可以幫我嗎?

您可以簡化並使用以下內容打開您的文件:

Public Sub OpenCsvFile(byVal filepath)
    Workbooks.OpenText filename:=filepath, dataType:=xlDelimited, semicolon:=True
End Sub

有關此方法的更多詳細信息 (MSDN 鏈接)

你在這里有一個很好的解釋:

https://sitestory.dk/excel_vba/csv-file-import.htm

使用我在案例中應用的代碼:

 Sub OpenCSV()

 Dim sPath As String
 sPath = ThisWorkbook.Path & "\Site_survey_form2.csv"
 Workbooks.OpenText Filename:= _
 sPath, DataType:=xlDelimited, Semicolon:=True, Local:=True
 End Sub

因此,.csv 文件將在單獨的工作簿中打開。

如果您想立即將 .csv 內容導入您的工作簿,那么我建議您參考此處的線程:

有沒有辦法將數據從 .csv 導入到活動的 Excel 工作表中?

我從那里准備了以下代碼:

 Sub CSV_Import()
 Dim ws As Worksheet, strFile As String, sPath As String

 Set ws = ActiveWorkbook.Sheets("Sheet1") 'set to current worksheet name

 sPath = ThisWorkbook.Path & "\Site_survey_form2.csv"  '"\ your file name

  With ws.QueryTables.Add(Connection:="TEXT;" & sPath, 
  Destination:=ws.Range("A1"))  
 .TextFileParseType = xlDelimited
 .TextFileCommaDelimiter = True
 .Refresh

以結束子結束

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM