将 CSV 导入 Excel

Importing CSV to Excel

您好,我正在尝试将 csv 导入到 dumpWS。 当我取消文件打开时,此代码工作正常,但当我 select 然后 csv.file 打开时。这段代码给我 运行-time error '13' type mismatch.

有问题的行是"If strFile = False Then"

私人订阅 CSV_Import()

Dim addDumpWS As Worksheet
Dim dumpWS As Worksheet, strFile As String

Set dumpWS = ThisWorkbook.Worksheets("DUMP")

' Clear all data in worksheet(DUMP) before import
dumpWS.Cells.Clear

strFile = Application.GetOpenFilename("CSV Files (*.csv),*.csv", , "Please select text file...")

' Message box when file is not selected
If strFile = False Then
    ThisWorkbook.Worksheets("List").Select
    Range("ImportStatus").Select
    MsgBox "No file selected. Cannot continue import."
    Exit Sub

Else
' Import select csv
    With dumpWS.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=dumpWS.Range("A1"))
         .TextFileParseType = xlDelimited
         .TextFileCommaDelimiter = True
         .Refresh
    End With

' Copy imported data to WebDBDataBody
dumpWS.Activate
Rows(1).EntireRow.Delete
ActiveSheet.UsedRange.Select
Selection.Copy

Range("WebDBDataBody").PasteSpecial xlPasteValues

' Recorded Imported Date
Range("ImportedDate").Value = Date
End If

结束子

我改了

如果 strFile = False 那么

至 如果 strFile = Cstr(False) 那么

而且效果很好