清楚工作表和工作簿的区别,下面的代码可以把同文件夹下的CSV文件复制到一个工作簿的不同工作表中。但在EXCEL2003中工作表的数量不能超过256个,否则会出错。
Private Sub copy_csvfile_to_excel()
Dim MyPath$, myFile$, AK As WorkbookApplication.ScreenUpdating = False
MyPath = ThisWorkbook.Path & "\"
myFile = Dir(MyPath & "*.csv")
Do While myFile <> ""
If myFile <> ThisWorkbook.Name Then
Set AK = Workbooks.Open(MyPath & myFile)
AK.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Workbooks(myFile).Close
End If
myFile = Dir
Set AK = Nothing
Loop
Application.ScreenUpdating = True
ActiveWorkbook.Save
MsgBox "汇总完成,请查看!", 64, "提示"
End Sub