求,大师帮忙,写个excel vba代码 多个工作簿合并,把数据合并到一个新

的工作簿中,要从第3行到300行的一个复制区间,列可以不管。
2024-12-18 03:43:53
推荐回答(1个)
回答1:

Sub CombineWorkbooks()
Dim FilesToOpen, ft
Dim x As Integer
Application.ScreenUpdating = False
On Error GoTo errhandler

FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Micrsofe Excel文件(*.xlsx), *.xls", _
MultiSelect:=True, Title:="要合并的文件")

If TypeName(FilesToOpen) = "boolean" Then
MsgBox "没有选定文件"
'GoTo errhandler
End If

x = 1
While x <= UBound(FilesToOpen)
Set wk = Workbooks.Open(Filename:=FilesToOpen(x))

wk.Sheets().Move after:=ThisWorkbook.Sheets _
(ThisWorkbook.Sheets.Count)
x = x + 1
Wend

MsgBox "合并成功完成!"

errhandler:
' MsgBox Err.Description
'Resume errhandler
End Sub