VBA一鍵匯總多個(gè)工作簿-名稱(chēng)相同的工作表-的指定區(qū)域數(shù)據(jù)
日常工作,,我們經(jīng)常需要匯總相同格式的工作簿的某個(gè)工作表的數(shù)據(jù)
如1月業(yè)績(jī)、2月業(yè)績(jī),。,。。,。12月業(yè)績(jī)等
姓名 | 數(shù)量 | 數(shù)據(jù)22 | 22 | 數(shù)據(jù)23 | 23 | 數(shù)據(jù)24 | 24 | 數(shù)據(jù)25 | 25 | 數(shù)據(jù)26 | 26 | 數(shù)據(jù)27 | 27 | 數(shù)據(jù)28 | 28 | 數(shù)據(jù)29 | 29 | 數(shù)據(jù)30 | 30 | 數(shù)據(jù)31 | 31 | 數(shù)據(jù)32 | 32 | 數(shù)據(jù)33 | 33 | 數(shù)據(jù)34 | 34 | 數(shù)據(jù)35 | 35 | 數(shù)據(jù)36 | 36 | 數(shù)據(jù)37 | 37 | 數(shù)據(jù)38 | 38 | 數(shù)據(jù)39 | 39 | 數(shù)據(jù)40 | 40 |
VBA匯總后變成這樣:
啥也不說(shuō)了,,直接拿代碼去用
Dim 所有工作簿列表 As FileDialogSelectedItems Private Sub 提取數(shù)據(jù)按鈕_Click(sender As Object, e As EventArgs) Handles 提取數(shù)據(jù)按鈕.Click Dim dic As Object = CreateObject("scripting.dictionary") Dim sht As Excel.Worksheet With App.FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker) 所有工作簿列表 = .SelectedItems '記錄所有工作簿,防止二次選擇工作簿 For Each 工作簿路徑 As String In .SelectedItems wb = App.Workbooks.Open(工作簿路徑) For Each sht In wb.Worksheets ComboBox2.Items.Add(表名) '適合匯總同工作表名稱(chēng)的匯總 Private Sub 匯總數(shù)據(jù)按鈕_Click(sender As Object, e As EventArgs) Handles 匯總數(shù)據(jù)按鈕.Click Dim sht As Excel.Worksheet Dim 開(kāi)始輸出單元格 As Excel.Range = App.InputBox("請(qǐng)選擇開(kāi)始輸出單元格", Type:=8) Dim 輸出表 As Excel.Worksheet = App.ActiveSheet For Each 工作簿路徑 As String In 所有工作簿列表 wb = App.Workbooks.Open(工作簿路徑) For Each sht In wb.Worksheets If sht.Name = ComboBox2.Text Then Dim lastrow As Long = 輸出表.Cells(輸出表.Rows.Count, 開(kāi)始輸出單元格.Column).end(Microsoft.Office.Interop.Excel.XlDirection.xlUp).row + 1 '最后一行 sht.Range(ComboBox3.Text).Copy(輸出表.Cells(lastrow, 開(kāi)始輸出單元格.Column)) '盡量不要整列,,否則可能出錯(cuò) Private Sub 選擇單元格按鈕_Click(sender As Object, e As EventArgs) Handles 選擇單元格按鈕.Click ComboBox3.Text = App.InputBox("請(qǐng)選擇匯總區(qū)域", Type:=8).address
我的窗體界面是這樣的
希望大家多多支持,!謝謝
|