这段代码工作到一周前,然后决定不再感谢电子表格没有改变,也没有导入csv文件。
我已经列出了err消息下面的代码,因为文件导入是1004文件不存在但是文件在正确的目录中
Sub ImportCSV(SheetName As String, FiletoImport As String)
On Error GoTo errorhandler
Dim ws As Worksheet, strFile As String, msgtext As String, response
Sheets(SheetName).Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
CreateSheet:
Sheets.Add After:=Sheets(Sheets.Count)
ActiveWorkbook.ActiveSheet.Name = SheetName
Set ws = ActiveWorkbook.Sheets(SheetName)
On Error GoTo importerror
' strFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please selec text file...")
With ws.QueryTables.Add(Connection:="TEXT;" & FiletoImport, _
Destination:=ws.Range("$A$1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
' .Refresh
End With
Select Case SheetName
Case "POData"
Call ProcessPOData
Case "SOData"
Call ProcessSOData
Call DeleteRows
Case "AvgSales"
Call CreateAvgSales
End Select
Exit Sub
errorhandler:
MsgBox Err & ": " & Error(Err)
Resume CreateSheet
importerror:
MsgBox Err & ": " & Error(Err)
msgtext = "Failed to Import " & FiletoImport
response = MsgBox(msgtext, vbCritical, "Import File Failed")
End Sub