将多个csv文件导入到工作簿中的特定工作表

时间:2019-06-13 08:47:21

标签: excel vba csv-import

我在位于Z:\ Data的文件夹中有三个名为CAT.csv,BAT.csv和MAT.csv的csv文件。我希望将这些csv文件导入到名为“每日检查”的工作簿中,该工作簿具有名为CAT,BAT和MAT的工作表。我希望使用宏将CAT.csv文件导入到CAT工作表中,并将BAT.csv文件导入到BAT工作表中,并将MAT.csv文件导入到工作簿“每日检查”中的MAT工作表中。除此之外,如果在文件夹中未找到任何csv文件,则消息框应显示为“找不到文件”,还应指定未找到哪个文件,然后应运行其余代码,并应导入其他文件工作。我有一个可以导入一个csv文件的代码,但无法对其进行修改以适用于所有csv文件。

Sub ImportCSV()
    Const strFileName = "Z:\Data\CAT.csv"
    If Dir$("C:\Users\mbollineni\Desktop\To do\New folder\CAT.csv") = "" Then
   MsgBox "CAT File not found"
  End If
  Dim wbkS As Workbook
  Dim wshS As Worksheet
  Dim wshT As Worksheet
  Set wshT = Worksheets("CAT")
      Set wbkS = Workbooks.Open(fileName:=strFileName)
  Set wshS = wbkS.Worksheets(1)
  wshS.UsedRange.Copy Destination:=wshT.Range("A1")`enter code here`
  wbkS.Close SaveChanges:=False
End Sub

2 个答案:

答案 0 :(得分:2)

与下面类似,将其复制一次以获取最后一个文件。

Sub ImportCSV()

' For CAT File

    strFileName = "Z:\Data\CAT.csv"
    If dir$("C:\Users\mbollineni\Desktop\To do\New folder\CAT.csv") = "" Then
   MsgBox "CAT File not found"
  End If
  Dim wbkS As Workbook
  Dim wshS As Worksheet
  Dim wshT As Worksheet
  Set wshT = Worksheets("CAT")
      Set wbkS = Workbooks.Open(Filename:=strFileName)
  Set wshS = wbkS.Worksheets(1)
  wshS.UsedRange.Copy Destination:=wshT.Range("A1")
  wbkS.Close SaveChanges:=False

' For MAT file

    strFileName = "Z:\Data\MAT.csv"
    If dir$("C:\Users\mbollineni\Desktop\To do\New folder\MAT.csv") = "" Then
   MsgBox "MAT File not found"
  End If
  Dim wbkS As Workbook
  Dim wshS As Worksheet
  Dim wshT As Worksheet
  Set wshT = Worksheets("MAT")
      Set wbkS = Workbooks.Open(Filename:=strFileName)
  Set wshS = wbkS.Worksheets(1)
  wshS.UsedRange.Copy Destination:=wshT.Range("A1")
  wbkS.Close SaveChanges:=False




End Sub

答案 1 :(得分:0)

我不会使用.Copy,但我不想更改任何逻辑:

Public Enum FileToImport
    [_Start] = 1
        CAT = 1
        BAT = 2
        MAT = 3
    [_End] = 3
End Enum

Function WorksheetToUse(FileNumber As FileToImport) As Worksheet
    Dim wsName As String
    Select Case FileNumber
        Case FileToImport.CAT: wsName = "CAT"
        Case FileToImport.BAT: wsName = "BAT"
        Case FileToImport.MAT: wsName = "MAT"
    End Select
    Set WorksheetToUse = ThisWorkbook.Worksheets(wsName)
End Function

Function FileNameToUse(FileNumber As FileToImport) As String
    Select Case FileNumber
        Case FileToImport.CAT: FileNameToUse = "CAT.csv"
        Case FileToImport.BAT: FileNameToUse = "BAT.csv"
        Case FileToImport.MAT: FileNameToUse = "MAT.csv"
    End Select
End Function

Function FileExists(strFullPath As String) As Boolean
    FileExists = Dir(strFullPath) <> ""
End Function

Sub ImportCSV()
    Const strDefaultFolder As String = "Z:\Data\"
    Dim wbkS As Workbook
    Dim wshS As Worksheet
    Dim wshT As Worksheet
    Dim lCounter As Long
    Dim strErr As String
    Dim strFullPath As String

    For lCounter = FileToImport.[_Start] To FileToImport.[_End]
        strFullPath = strDefaultFolder & FileNameToUse(lCounter)
        If FileExists(strFullPath) Then
            Set wshT = WorksheetToUse(lCounter)
            Set wbkS = Workbooks.Open(strfullname)
            Set wshS = wbkS.Worksheets(1)
            wshS.UsedRange.Copy Destination:=wshT.Range("A1")
            wbkS.Close SaveChanges:=False
        Else
            strErr = strErr & vbNewLine & FileNameToUse(lCounter) & " not found"
        End If
    Next lCounter

    If strErr <> "" Then
        MsgBox Mid(strErr, 3), vbExclamation, "File not found!"
    End If
End Sub