【问题标题】:Select only multiple csv files and make tabbed xlsx file gets error 52仅选择多个 csv 文件并使选项卡式 xlsx 文件出现错误 52
【发布时间】:2015-08-02 15:12:37
【问题描述】:

我正在尝试导航到具有 GetFilesUserForm 功能的文件夹,然后多选 csv 文件,然后使用子 ImportCombineCSVsNavigate 创建一个多选项卡式 xlsx 文件

我认为函数 GetFilesUserForm 正在工作,但 ImportCombineCSVsNavigate 期待一个未选择文件的文件夹我收到错误 52Bad file name or number 没有得到如何更改它以使用选定的文件

谢谢

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

【问题讨论】:

    标签: excel filepicker vba


    【解决方案1】:

    您如何称呼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
    

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 2021-08-09
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2018-04-13
      相关资源
      最近更新 更多