Select 只有多个 csv 文件并使选项卡式 xlsx 文件出现错误 52

Select only multiple csv files and make tabbed xlsx file gets error 52

我正在尝试使用函数 GetFilesUserForm 导航到一个文件夹,然后导航到多个 select csv 文件,然后使用 Sub ImportCombineCSVsNavigate 创建一个多选项卡 xlsx 文件

我认为函数 GetFilesUserForm 正在运行,但 ImportCombineCSVsNavigate 需要一个文件夹而不是 selected 文件 我得到错误 52Bad file name or number 没有得到如何更改它以使用 selected 文件

谢谢

Option Explicit
Public fPath As String


Sub ImportCombineCSVsNavigate()
'Summary:   Import all CSV files from a folder into separate sheets named    for the CSV filenames
Dim fCSV    As String
Dim wbCSV   As Workbook

'start the CSV file listing
fCSV = Dir(fPath & "*.csv")
Do While Len(fCSV) > 0
    'open a CSV file and move
    Set wbCSV = Workbooks.Open(fPath & fCSV)
    ActiveSheet.Move Before:=ThisWorkbook.Sheets("Helper")
    'ActiveSheet.Move After:=ThisWorkbook.Sheets(Sheets.Count)

    'ready next CSV
    fCSV = Dir
Loop


Set wbCSV = Nothing
End Sub



Function GetFilesUserForm() As String
Dim fd As FileDialog
Dim FileChosen As Integer
Dim filter As String, strPath As String


Set fd = Application.FileDialog(msoFileDialogFilePicker)
strPath = "C:Desktop"
With fd
    .AllowMultiSelect = True
    .Filters.Add "CSV Files", "*.*", 1
    .FilterIndex = 2
    .Title = "Choose CSV File"
    .InitialView = msoFileDialogViewDetails
    '.Show

 FileChosen = fd.Show
   If FileChosen <> -1 Then
      'didn't choose anything (clicked on CANCEL)
       MsgBox "You chose cancel"
       End
   Else

      'display name and path of file chose
        GetFilesUserForm = fd.SelectedItems(1)

   End If
 End With
End Function

如何称呼 ImportCombineCSVsNavigate() 子?目前它不需要任何参数。如果您将 GetFilesUserForm()return 设为 FileDialogueSelectedItems,您可以将其传递到 ImportCombineCSVsNavigate() 子项中。

Sub Test()

    Application.ScreenUpdating = False

    Call ImportCombineCSVsNavigate(GetFilesUserForm())

    Application.ScreenUpdating = True

End Sub

Sub ImportCombineCSVsNavigate(files As FileDialogSelectedItems)

    'Summary:   Import all CSV files from a folder into separate sheets named    for the CSV filenames
    Dim wbCSV   As Workbook

    Dim i As Integer
    For i = 1 To files.Count
        Set wbCSV = Workbooks.Open(files(i))
        ActiveSheet.Move Before:=ThisWorkbook.Sheets("Helper")
    Next i

    Set wbCSV = Nothing

End Sub

Function GetFilesUserForm() As FileDialogSelectedItems

    Dim fd As FileDialog
    Dim FileChosen As Integer
    Dim filter As String, strPath As String


    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    strPath = "C:Desktop"
    With fd
        .AllowMultiSelect = True
        .Filters.Add "CSV Files", "*.csv", 1
        .FilterIndex = 0
        .Title = "Choose CSV File"
        .InitialView = msoFileDialogViewDetails
        '.Show
    End With

    FileChosen = fd.Show
    If FileChosen <> -1 Then
      'didn't choose anything (clicked on CANCEL)
       MsgBox "You chose cancel"
       End
    End If

    'return all the files together as FileDialogSelectedItems
    Set GetFilesUserForm = fd.SelectedItems

End Function