情況一:單文件多工作表合并,即在一張工作薄中,有多個工作表格,每個表格的內(nèi)容都一致,只是所屬的類別不同?,F(xiàn)在要將所有類別表格里的內(nèi)容全部合并到一張工作表格里。如以下表格(諾基亞零配件清單),一共有200多種型號,每種型號一個清單,現(xiàn)在要將它們?nèi)亢喜⒌揭粡埞ぷ鞅砀窭铩?/p>
解決方案:插入一張工作表格,命名為“匯總”。按Alt+F11,進(jìn)入VBA編輯器,寫上如下代碼:
04 | Private beginRowNo As Long |
07 | Private Sub CommandButton1_Click() |
08 | Dim sheetCount As Integer |
09 | sheetCount = ThisWorkbook.Worksheets.Count |
14 | For i = 1 To sheetCount |
15 | Dim sheetName As String |
16 | sheetName = ThisWorkbook.Worksheets(i).Name |
18 | Select Case LCase(sheetName) |
20 | MsgBox "跳過 " + sheetName |
22 | MsgBox "跳過 " + sheetName |
24 | MsgBox "跳過 " + sheetName |
26 | DoSubtotal (sheetName) |
32 | Private Sub DoSubtotal( ByVal sheetName As String ) |
33 | Dim sourceSheet As Worksheet |
34 | Dim destSheet As Worksheet |
36 | Set sourceSheet = ThisWorkbook.Worksheets(sheetName) |
37 | Set destSheet = ThisWorkbook.Worksheets( "匯總" ) |
39 | sourceSheet.UsedRange.Copy |
40 | destSheet.Range( "A" & beginRowNo).PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, True |
41 | beginRowNo = beginRowNo + sourceSheet.UsedRange.Rows.Count |
43 | Set sourceSheet = Nothing |
44 | Set destSheet = Nothing |
然后,將光標(biāo)放置在 CommandButton1_Click 過程中的任意位置,按F5運(yùn)行即可。
情況二:多文件合并,即在一個文件夾里,有多個工作薄文件,它們的第一個表格里的內(nèi)容形式都一樣,現(xiàn)在要將它們?nèi)亢喜⒌揭粋€工作薄里。如一個文件夾內(nèi),有每天的訂單Excel文件,現(xiàn)在要將全部訂單數(shù)據(jù)合并到一個Excel文件內(nèi)。
解決方案:新建一個Excel工作薄,按Alt+F11,進(jìn)入VBA編輯器,輸入如下代碼:
05 | On Error GoTo ErrHandler |
06 | Application.ScreenUpdating = False |
08 | FilesToOpen = Application.GetOpenFilename _ |
09 | (FileFilter:= "Microsoft Excel Files (*.xls), *.xls" , _ |
10 | MultiSelect:= True , Title:= "Files to Merge" ) |
12 | If TypeName(FilesToOpen) = "Boolean" Then |
13 | MsgBox "No Files were selected" |
18 | Dim currentWorkSheet As Worksheet |
20 | Set currentWorkSheet = ActiveWorkbook.ActiveSheet |
21 | Set rng = currentWorkSheet.Range( "A1" ) |
25 | While x <= UBound(FilesToOpen) |
26 | Set wkb = Workbooks.Open(Filename:=FilesToOpen(x)) |
27 | Set wks = wkb.Worksheets(1) |
29 | rng.Offset(0, 10).Value = wkb.Name |
31 | wks.UsedRange.Copy rng |
33 | Set rng = rng.Offset(wks.UsedRange.Rows.Count, 0) |
41 | Set currentWorkSheet = Nothing |
44 | Application.ScreenUpdating = True |
48 | MsgBox Err.Description |
將光標(biāo)放在過程“合并工作薄”的任意位置,按F5運(yùn)行,在彈出的打開文件框中,選擇需要合并的全部文件,確定即可。
情況三:多文件合并。類似情況二,但是,只將多個工作薄里的工作表復(fù)制到同一個工作薄里,不需要到同一個工作表。
解決方案:類似情況二,代碼只有一點(diǎn)點(diǎn)區(qū)別:
05 | On Error GoTo ErrHandler |
06 | Application.ScreenUpdating = False |
08 | FilesToOpen = Application.GetOpenFilename _ |
09 | (FileFilter:= "Microsoft Excel Files (*.xls), *.xls" , _ |
10 | MultiSelect:= True , Title:= "Files to Merge" ) |
12 | If TypeName(FilesToOpen) = "Boolean" Then |
13 | MsgBox "No Files were selected" |
19 | While x <= UBound(FilesToOpen) |
20 | Workbooks.Open Filename:=FilesToOpen(x) |
22 | Sheets().Move After:=ThisWorkbook.Sheets _ |
23 | (ThisWorkbook.Sheets.Count) |
29 | Application.ScreenUpdating = True |
33 | MsgBox Err.Description |
注:如果先做情況三,再做情況一,那么就等于情況二。