thanhlanh
Thành viên tích cực
- Tham gia
- 21/2/08
- Bài viết
- 858
- Được thích
- 1,211
Nếu như bạn có nhiều File Excel mà muốn nối lại thành một File duy nhất . Ví dụ File A.XLS có 2 Sheet , File B.XLS có 3 Sheet muốn nối thành File C có 5 Sheet cách thức tiến hành như sau .
Sub CombineWorkbooks()
Dim FilesToOpen
Dim x As Integer
On Error GoTo ErrHandler
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xls), *.xls", MultiSelect:=True, Title:="Files to Merge")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
While x <= UBound(FilesToOpen)
Workbooks.Open Filename:=FilesToOpen(x)
Sheets().Move After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
- Mở File C , là nơi sẽ gộp tất cả các Sheet của File A.XLS và B.XLS thành một File , bấm Alt_F11 .
- Cửa sổ Microsoft Visual Basic mở ra , bấm menu Insert > Module .
Sub CombineWorkbooks()
Dim FilesToOpen
Dim x As Integer
On Error GoTo ErrHandler
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xls), *.xls", MultiSelect:=True, Title:="Files to Merge")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
While x <= UBound(FilesToOpen)
Workbooks.Open Filename:=FilesToOpen(x)
Sheets().Move After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
- Quay trở lại Excel
- Bấm menu Tool > Macro > Macro , bạn sẽ chọn CombineWorkbooks và bấm Run
- Cửa sổ File to Merge mở ra , bạn hãy chọn những File mà muốn nối bằng cách giữ phím CTRL và chọn những File này , sau đó bấm Open