0
嗨我試圖導入csv到dumpWS。 當我取消文件打開此代碼工作正常,但是當我選擇csv.file然後打開。此代碼給我運行時錯誤'13'類型錯配。導入CSV到Excel
它有問題的行是 「如果strFile = false,那麼」
私人小組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可能被聲明爲一個字符串,對嗎?假是布爾數據,而不是字符串。 – user3479671 2015-02-05 15:58:49
我想你是對的。正如我研究,我發現了兩個解決方案。將strFile更改爲變體或將狀態更改爲Cstr(Flase)。 – 2015-02-05 16:22:17