excel如何将一个工作簿中的多个工作表合并到一张工作表上?

 我来答
言南絮
高粉答主

推荐于2019-09-29 · 繁杂信息太多,你要学会辨别
知道小有建树答主
回答量:187
采纳率:100%
帮助的人:30万
展开全部
  1. 原始数据所在工作簿包含多个格式相同的工作表,只不过每个工作表内容不同,比如说不同人名的工作表数据或者不同部门填写的数据;

  2. 在原始数据同目录下新建一个工作簿,建立两个工作表,名称分别为“首页”和“合并汇总表”;

  3. 按Alt+F11进入VBA代码编辑和调试界面;

  4. 根据下图提示,插入一个模块;

  5. 将下述代码粘贴到模块空白处:

    Sub CombineSheetsCells()

    Dim wsNewWorksheet As Worksheet

    Dim cel As Range

    Dim DataSource, RowTitle, ColumnTitle, SourceDataRows, SourceDataColumns As Variant

    Dim TitleRow, TitleColumn As Range

    Dim Num As Integer

    Dim DataRows As Long

    DataRows = 1

    Dim TitleArr()

    Dim Choice

    Dim MyName$, MyFileName$, ActiveSheetName$, AddressAll$, AddressRow$, AddressColumn$, FileDir$, DataSheet$, myDelimiter$

    Dim n, i

    n = 1

    i = 1

    Application.DisplayAlerts = False

    Worksheets("合并汇总表").

    DeleteSet wsNewWorksheet = Worksheets.Add(, after:=Worksheets(Worksheets.Count))

    wsNewWorksheet.Name = "合并汇总表"

    MyFileName = Application.GetOpenFilename("Excel工作薄 (*.xls*),*.xls*")

    If MyFileName = "False" Then

    MsgBox "没有选择文件!请重新选择一个被合并文件!", vbInformation, "取消"

    Else

    Workbooks.Open Filename:=MyFileName

    Num = ActiveWorkbook.Sheets.Count

    MyName = ActiveWorkbook.Name

    Set DataSource = Application.InputBox(prompt:="请选择要合并的数据区域:", Type:=8)

    AddressAll = DataSource.Address

    ActiveWorkbook.ActiveSheet.Range(AddressAll).Select

    SourceDataRows = Selection.Rows.Count

    SourceDataColumns = Selection.Columns.Count

    Application.ScreenUpdating = False

    Application.EnableEvents = False

    For i = 1 To Num

    ActiveWorkbook.Sheets(i).Activate

    ActiveWorkbook.Sheets(i).Range(AddressAll).Select

    Selection.Copy

    ActiveSheetName = ActiveWorkbook.ActiveSheet.Name

    Workbooks(ThisWorkbook.Name).Activate

    ActiveWorkbook.Sheets("合并汇总表").Select

    ActiveWorkbook.Sheets("合并汇总表").Range("A" & DataRows).Value = ActiveSheetName

    ActiveWorkbook.Sheets("合并汇总表").Range(Cells(DataRows, 2), Cells(DataRows, 2)).Select

    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone,

    SkipBlanks:=False, Transpose:=False

    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _

    False, Transpose:=False

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=False

    DataRows = DataRows + SourceDataRows

    Workbooks(MyName).Activate

    Next i

    Application.ScreenUpdating = True

    Application.EnableEvents = True

    End If

    Workbooks(MyName).Close

    End Sub

  6. 在“首页”工作表中按下图示范插入一个窗体控件并指定宏为插入的代码名称;

  7. 点击“首页”工作表中插入的按钮,根据提示,浏览到原始数据工作簿;

  8. 下一步,用鼠标选择要合并的数据范围;

  9. 点击确定按钮,待代码运行完毕后,所有的数据就都合并到了“合并汇总表”中;

  10. 选中全部数据区域,执行自动筛选。然后选择其中一个字段,选择“空白”和标题内容;

  11. 然后将筛选出来的无用行鼠标右键删除,再删除A列整列即可。

幽月半遣
2020-01-15
知道答主
回答量:25
采纳率:100%
帮助的人:3.3万
展开全部
楼上代码格式有点BUG,不懂代码的同学可以复制我的,步骤跟楼上一样

Sub CombineSheetsCells()

Dim wsNewWorksheet As Worksheet
Dim cel As Range
Dim DataSource, RowTitle, ColumnTitle, SourceDataRows, SourceDataColumns As Variant
Dim TitleRow, TitleColumn As Range
Dim Num As Integer
Dim DataRows As Long
DataRows = 1
Dim TitleArr()
Dim Choice
Dim MyName$, MyFileName$, ActiveSheetName$, AddressAll$, AddressRow$, AddressColumn$, FileDir$, DataSheet$, myDelimiter$
Dim n, i
n = 1
i = 1
Application.DisplayAlerts = False
Worksheets("合并汇总表").Delete
Set wsNewWorksheet = Worksheets.Add(, after:=Worksheets(Worksheets.Count))
wsNewWorksheet.Name = "合并汇总表"

MyFileName = Application.GetOpenFilename("Excel工作薄 (*.xls*),*.xls*")

If MyFileName = "False" Then
MsgBox "没有选择文件!请重新选择一个被合并文件!", vbInformation, "取消"

Else

Workbooks.Open Filename:=MyFileName

Num = ActiveWorkbook.Sheets.Count
MyName = ActiveWorkbook.Name
Set DataSource = Application.InputBox(prompt:="请选择要合并的数据区域:", Type:=8)
AddressAll = DataSource.Address
ActiveWorkbook.ActiveSheet.Range(AddressAll).Select

SourceDataRows = Selection.Rows.Count
SourceDataColumns = Selection.Columns.Count
Application.ScreenUpdating = False
Application.EnableEvents = False

For i = 1 To Num
ActiveWorkbook.Sheets(i).Activate
ActiveWorkbook.Sheets(i).Range(AddressAll).Select
Selection.Copy

ActiveSheetName = ActiveWorkbook.ActiveSheet.Name
Workbooks(ThisWorkbook.Name).Activate
ActiveWorkbook.Sheets("合并汇总表").Select

ActiveWorkbook.Sheets("合并汇总表").Range("A" & DataRows).Value = ActiveSheetName
ActiveWorkbook.Sheets("合并汇总表").Range(Cells(DataRows, 2), Cells(DataRows, 2)).Select

Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

DataRows = DataRows + SourceDataRows

Workbooks(MyName).Activate

Next i

Application.ScreenUpdating = True

Application.EnableEvents = True

End If

Workbooks(MyName).Close
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
princepiaosha
2019-09-16
知道答主
回答量:1
采纳率:0%
帮助的人:693
展开全部
这个程序有点bug 该怎么调试?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式