excel vba 将多张数据表的内容合并到一张数据表

功能描述:

一个Excel文件有很多个 样式相同 的数据表,

需要将多张数据表的内容合并到一张数据表里。

vba实现代码如下:

vbnet 复制代码
Attribute VB_Name = "NewMacros"
Option Explicit
Public Const Const_OutSheetName As String = "VBA汇总"
Public Const Const_PZSheetName As String = "配置"

Sub 汇总()
Attribute 汇总.VB_Description = "宏由 LiuZW 录制,时间: 2023/08/19"
Attribute 汇总.VB_ProcData.VB_Invoke_Func = " 14"
'
' 汇总 Macro
' 宏由 LiuZW 录制,时间: 2023/08/19
'

'

    Dim i, j, k As Integer
        
    
    '创建"配置"数据表并提示用户填写配置
    Dim isExistPZ As Boolean
    isExistPZ = False
    For i = 1 To Worksheets.Count
        If Worksheets(i).name = Const_PZSheetName Then
            isExistPZ = True
            Exit For
        End If
    Next
    
    '定义表示要复制的区域的变量
    Dim mRow1, mColumn1, mRow2, mColumn2 As Integer
        
    If isExistPZ Then
        mRow1 = Application.Worksheets(Const_PZSheetName).Range("B2").Value
        mRow2 = Application.Worksheets(Const_PZSheetName).Range("B3").Value
        mColumn1 = Application.Worksheets(Const_PZSheetName).Range("B4").Value
        mColumn2 = Application.Worksheets(Const_PZSheetName).Range("B5").Value
       
       If mRow1 = 0 Or mRow2 = 0 Or mColumn1 = 0 Or mColumn2 = 0 Then
       
            '提示用户填写
            MsgBox ("请填写配置数据表后运行。")
            Exit Sub
       End If
       
       '配置的填写有效性判断
       If Not IsNumeric(mRow1) Or Not IsNumeric(mRow2) Or Not IsNumeric(mColumn1) Or Not IsNumeric(mColumn2) Then
            MsgBox ("配置数据表中键入的区域表述无效,请键入数字格式的行列号。")
            Exit Sub
       End If
       
    
    Else
        '创建"配置"数据表
        Sheets.Add
        ActiveSheet.name = Const_PZSheetName
        
        '填写基础信息
        Application.Worksheets(Const_PZSheetName).Range("A1").Value = "不需要汇总的数据表名称"
        Application.Worksheets(Const_PZSheetName).Range("B1").Value = Const_PZSheetName
        Application.Worksheets(Const_PZSheetName).Range("C1").Value = Const_OutSheetName
        Application.Worksheets(Const_PZSheetName).Range("A2").Value = "复制区域的起始行"
        Application.Worksheets(Const_PZSheetName).Range("A3").Value = "复制区域的终止行"
        Application.Worksheets(Const_PZSheetName).Range("A4").Value = "复制区域的起始列"
        Application.Worksheets(Const_PZSheetName).Range("A5").Value = "复制区域的终止列"
        
        '提示用户填写
        MsgBox ("请填写配置数据表后运行。")
        Exit Sub
    End If
    
    '判断是否已有"VBA汇总"数据表
    For i = 1 To Worksheets.Count
        If Worksheets(i).name = Const_OutSheetName Then
            MsgBox ("要生成的数据表"" + Const_OutSheetName + ""存在同名数据表,请修改或删除同名数据表后重试。")
            Exit Sub
        End If
    Next
    
    '创建"VBA汇总"数据表
    Sheets.Add
    ActiveSheet.name = Const_OutSheetName
    Columns("A:A").Select
    Selection.NumberFormatLocal = "@"
      
    '复制各个数据表的数据并粘贴到汇总表
    For i = 1 To Worksheets.Count
        Dim mSheetName As String
        mSheetName = Worksheets(i).name
        
        '判断当前数据表是否为 无需汇总的数据表
        'MsgBox ("当前数据表的第一行一共有" + CStr(Application.CountA(Sheets(Const_PZSheetName).Rows(1))) + "个数据")
        
        '定义当前数据表是否为 无需汇总的数据表 的标记,True表示无需汇总,False表示需要汇总
        Dim mKey As Boolean
        mKey = False
        
        For j = 2 To Application.CountA(Sheets(Const_PZSheetName).Rows(1))
            If mSheetName = Sheets(Const_PZSheetName).Cells(1, j) Then
                'MsgBox ("当前数据表"" + mSheetName + ""是不需要汇总的数据表")
                mKey = True
                Exit For
            End If
        Next
        
        '如果当前数据表不是 无需汇总的数据表,就执行汇总
        If mKey = False Then
        
            '执行复制和粘贴
            
            
            
            Application.Worksheets(mSheetName).Activate
            Application.Worksheets(mSheetName).Range(Cells(mRow1, mColumn1), Cells(mRow2, mColumn2)).Select
            Selection.Copy
            
            '判断要粘贴的位置并粘贴
            Application.Worksheets(Const_OutSheetName).Activate
            Dim usableRowCount As Integer
            usableRowCount = Application.Application.Sheets(Const_OutSheetName).Range("b65536").End(xlUp).Row + 2
            Application.Worksheets(Const_OutSheetName).Cells(usableRowCount, 2).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlPasteSpecialOperationNone, SkipBlanks:=False, Transpose:=False
            
            '填充第一列
            For k = 0 To mRow2 - mRow1
                Application.Worksheets(Const_OutSheetName).Cells(usableRowCount + k, 1).Value = mSheetName
            Next
            
        End If
    Next
End Sub

文件链接:数据表合并.bas

下载后直接在excel 查看代码处导入文件即可。

相关推荐
大雨淅淅3 分钟前
【机器人】ROS2 机械臂控制(MoveIt2)从入门到实战
人工智能·python·神经网络·学习·算法·机器学习·机器人
狗凯之家源码网11 分钟前
电商代付系统从零搭建与实战指南
前端·后端·开源
小雨下雨的雨14 分钟前
通过鸿蒙PC Electron框架技术完成-井字棋游戏 - 实现详解
前端·javascript·游戏·华为·electron·鸿蒙
meilindehuzi_a16 分钟前
掌握 ES6 核心语法与大模型(NLP)项目工程化搭建指南
前端·自然语言处理·es6
IT_陈寒23 分钟前
Vue组件通信这个坑我跳了两次才知道怎么爬出来
前端·人工智能·后端
smallswan29 分钟前
第十四 算数运算
linux·服务器·前端
AI_零食30 分钟前
甄嬛人物日志-朗读升级 - 鸿蒙PC Electron框架完整技术实现指南
前端·学习·华为·electron·鸿蒙·鸿蒙系统
HackTwoHub30 分钟前
WEB扫描器Invicti-Professional-V26.50.0(自动化爬虫扫描)更新
前端·人工智能·chrome·爬虫·web安全·网络安全·自动化
copyer_xyf32 分钟前
Python 文件基本操作
前端·后端·python
智者知已应修善业33 分钟前
【51单片机0.1秒计时到21.0时点亮LED】2024-1-5
c++·经验分享·笔记·算法·51单片机