【发布时间】:2015-08-02 15:12:37
【问题描述】:
我正在尝试导航到具有 GetFilesUserForm 功能的文件夹,然后多选 csv 文件,然后使用子 ImportCombineCSVsNavigate 创建一个多选项卡式 xlsx 文件
我认为函数 GetFilesUserForm 正在工作,但 ImportCombineCSVsNavigate 期待一个未选择文件的文件夹我收到错误 52 或 Bad 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