1

这段代码在一周前工作,然后决定不提供任何帮助,非常感谢电子表格没有改变,也没有要导入的 csv 文件。

我在文件导入的错误消息下方列出了代码是 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
4

0 回答 0