我在一个文件夹中有多个(确切地说是5个).csv,并且我想制作一个宏,让我选择文件夹中的csv文件并将它们导入到启用宏的空白文件中。以下是我想要做的一步一步:
我尝试了许多不同的方法,但我不认为我走的是正确的道路。这是一个:
Sub convert_to_macro()
'This first line is crap though. It only lets you export it to a certain place
ChDir "C:\Users\pal\Documents\CMSe\Lucys Computer"
With Application.FileDialog(msoFileDialogFilePicker)
.Show
If .SelectedItems.Count = 0 Then
MsgBox "Cancel Selected"
Exit Sub
End If
something = .SelectedItems(1)
End With
ActiveWorkbook.SaveAs Filename:= _
"something.xltm", _
FileFormat:=xlOpenXMLTemplateMacroEnabled, Password:="", WriteResPassword _
:="", ReadOnlyRecommended:=False, CreateBackup:=False
End Sub
感谢您给予的任何帮助
答案 0 :(得分:0)
像这样将所有csv添加到单个文件中。
Sub convert_to_macro()
Dim Wb As Workbook
Dim Wb1 As Workbook
Dim fd As FileDialog
Dim strWB
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
'set initial directory to search
.InitialFileName = "c:\temp"
.Filters.Clear
.Filters.Add "csv files", "*.csv"
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No selection, exiting"
Exit Sub
End If
End With
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
Set Wb = Workbooks.Add(1)
For Each strWB In fd.SelectedItems
Set Wb1 = Workbooks.Open(strWB)
Wb1.Sheets(1).Copy after:=Wb.Sheets(Wb.Sheets.Count)
Wb1.Close False
Next
Wb.Sheets(1).Delete
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub