ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

EH搜索     
EH云课堂-专业的职场技能充电站 Excel转在线管理系统,怎么做看这里 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
Excel不给力? 何不试试FoxTable! Excel 2016函数公式学习大典 高效办公必会的Office实战技巧 免费下载Excel行业应用视频
300集Office 2010微视频教程 Tableau-数据可视化工具 精品推荐-800套精选PPT模板,点击获取 ExcelHome出品 - VBA代码宝免费下载
你的Excel 2010实战技巧学习锦囊 欲罢不能, 过目难忘的 Office 新界面 Excel VBA经典代码实践指南
查看: 1369|回复: 2

[原创] 多工作簿同结构表汇总的VB+EXCEL 公式方法

[复制链接]

TA的精华主题

TA的得分主题

发表于 2013-4-30 14:15 | 显示全部楼层 |阅读模式
最近,在工作中汇表,总结一种汇表思路:
1、基本思路:
     把各工作簿的表,粘贴到汇总表中。通过公式SUM(表一:表N!单元格)来汇总!
2、操作方法:
    1)建立模板汇总表,建立汇总表(汇总公式SUM(A:Z!单元格),“各名称”,“A”,“B”表
    2)vb+txt 实现各工作簿的工作表粘贴到指定工作簿汇总表中SHEET("A")和 sheet(“B”)之间
    3)把汇总表放到moudle 文件夹中,把各个独立的表放到“各单位明细” 文件夹中
    4)在setting 文件中的data 文本文件中写入   汇总表名称:明细表工作表名
    5)运行  表格汇总通用工具.EXE  即可
    6)到result 文件夹中查看结果
希望此工具能给您带来方便!

我的汇表工具.zip

311.73 KB, 下载次数: 103

TA的精华主题

TA的得分主题

发表于 2013-4-30 22:18 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-5-14 16:06 | 显示全部楼层
本帖最后由 a8737461 于 2013-5-14 16:08 编辑
dajiahaoxinku12 发表于 2013-4-30 22:18
感谢分享,能否分享下vb代码
以下就是我的主要代码了,其实就是把工作表移动到汇总工作簿中并命名
Sub Main()
Dim xlApp As Object
Dim sht As Worksheet
Dim wb As EXCEL.Workbook
Dim ZWB As EXCEL.Workbook
Dim sP, eP, WBPATH, wbname, listWb As String
Dim shtcount, wbcount As Integer
Dim TABLE, tableSht As String
On Error Resume Next
If Me.COM1 <> "" Then
TABLE = Trim(Split(Me.COM1, ":")(0))
tableSht = Trim(Split(Me.COM1, ":")(1))
End If
If Dir(App.Path & "\MODULE\" & TABLE & ".xls") = "" Then
MsgBox "对不起,你所选择的模板表不存在!"
Exit Sub
End If
Set xlApp = GetObject(, "excel.Application")
If Err Then
Else
For Each wb In xlApp.Workbooks
            listWb = listWb & vbCrLf & wb.FullName
Next
If MsgBox("目前打开的工作簿有:" & vbCrLf & listWb & vbCrLf & "将强行关闭!", vbYesNo) = vbYes Then
xlApp.Workbooks.Close
xlApp.Quit
Else
Exit Sub
End If
End If
Set xlApp = CreateObject("Excel.application")
'xlApp.Visible = True
AP = App.Path & "\MODULE\" & TABLE & ".xls"
sP = App.Path & "\result\" & TABLE & ".xls"
WBPATH = App.Path & "\各单位明细"
FileCopy AP, sP
Set ZWB = xlApp.Workbooks.Open(sP)
WBPATH = App.Path & "\各单位明细\"
f = Dir(WBPATH & "*.xls")
m = 2
Do While f > " "
'MsgBox (f)
J = 0
Set wb = Workbooks.Open(WBPATH & f)
wbname = wb.Name
DoEvents
lbl.Caption = "正在处理:" & wbname
DoEvents
'MsgBox (wb.Name)
shtcount = wb.Sheets.Count
For Each sht In wb.Sheets
If VBA.Trim(sht.Name) = tableSht Then
J = 1
sht.Name = Replace(wbname, ".xls", "")
'sht.Copy , AFTER:=ZWB.Sheets("A")
sht.Copy , ZWB.Sheets("A")
End If
Next
'判断是否存在
ZWB.Sheets("各名称").Cells(m, 2) = Replace(wbname, ".xls", "")
ZWB.Sheets("各名称").Cells(m, 1) = m - 1
If J = 0 Then
  ZWB.Sheets("各名称").Cells(m, 3) = "没有" & tableSht
Else
  ZWB.Sheets("各名称").Cells(m, 3) = "Yes"
End If
m = m + 1
'关闭工作表
wb.Close False
  
f = Dir
Loop
  Set sht = Nothing
ZWB.Close True
xlApp.Visible = True
xlApp.Quit
Set ZWB = Nothing
Set xlApp = Nothing
Me.lbl.Caption = "完成"
MsgBox "请到result文件夹中查看结果!"
Unload Me
End Sub

Sub Main()
Dim xlApp As Object
Dim sht As Worksheet
Dim wb As EXCEL.Workbook
Dim ZWB As EXCEL.Workbook
Dim sP, eP, WBPATH, wbname, listWb As String
Dim shtcount, wbcount As Integer
Dim TABLE, tableSht As String
On Error Resume Next
If Me.COM1 <> "" Then
TABLE = Trim(Split(Me.COM1, ":")(0))
tableSht = Trim(Split(Me.COM1, ":")(1))
End If
If Dir(App.Path & "\MODULE\" & TABLE & ".xls") = "" Then
MsgBox "对不起,你所选择的模板表不存在!"
Exit Sub
End If

Set xlApp = GetObject(, "excel.Application")
If Err Then

Else
For Each wb In xlApp.Workbooks
            listWb = listWb & vbCrLf & wb.FullName
Next
If MsgBox("目前打开的工作簿有:" & vbCrLf & listWb & vbCrLf & "将强行关闭!", vbYesNo) = vbYes Then
xlApp.Workbooks.Close
xlApp.Quit
Else
Exit Sub
End If
End If

Set xlApp = CreateObject("Excel.application")
'xlApp.Visible = True
AP = App.Path & "\MODULE\" & TABLE & ".xls"
sP = App.Path & "\result\" & TABLE & ".xls"
WBPATH = App.Path & "\各单位明细"
FileCopy AP, sP
Set ZWB = xlApp.Workbooks.Open(sP)

WBPATH = App.Path & "\各单位明细\"
f = Dir(WBPATH & "*.xls")
m = 2
Do While f > " "
'MsgBox (f)
J = 0
Set wb = Workbooks.Open(WBPATH & f)
wbname = wb.Name
DoEvents
lbl.Caption = "正在处理:" & wbname
DoEvents
'MsgBox (wb.Name)
shtcount = wb.Sheets.Count
For Each sht In wb.Sheets
If VBA.Trim(sht.Name) = tableSht Then
J = 1
sht.Name = Replace(wbname, ".xls", "")
'sht.Copy , AFTER:=ZWB.Sheets("A")
sht.Copy , ZWB.Sheets("A")

End If
Next
'判断是否存在
ZWB.Sheets("各名称").Cells(m, 2) = Replace(wbname, ".xls", "")
ZWB.Sheets("各名称").Cells(m, 1) = m - 1

If J = 0 Then
  ZWB.Sheets("各名称").Cells(m, 3) = "没有" & tableSht
Else
  ZWB.Sheets("各名称").Cells(m, 3) = "Yes"
End If
m = m + 1
'关闭工作表
wb.Close False
  
f = Dir
Loop

  Set sht = Nothing

ZWB.Close True
xlApp.Visible = True
xlApp.Quit
Set ZWB = Nothing
Set xlApp = Nothing
Me.lbl.Caption = "完成"
MsgBox "请到result文件夹中查看结果!"
Unload Me
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关注官方微信,高效办公专列,每天发车

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2019-7-24 06:09 , Processed in 0.081715 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2020 Wooffice Inc.

   

沪公网安备 31011702000001号 沪ICP备11019229号

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:徐怀玉律师 李志群律师

快速回复 返回顶部 返回列表