20180830xlVBA_合并计算
Sub WorkbooksSheetsConsolidate()
Rem 设置求和区域为 sheet名称/单元格区域;sheet名称/单元格区域
Const Setting As String = "Sheet1/A1:G6;Sheet1/A8:E8;Sheet1/F8:G8;Sheet2/A1:G3;Sheet2/A5:G5"
Const FOLDER_NAME As String = "文件夹"
Dim StartTime As Variant
Dim UsedTime As Variant
StartTime = VBA.Timer AppSettings True
Dim Wb As Workbook
Dim Sht As Worksheet
Dim Dic As Object
Dim Key As String
Dim OneKey
Dim Brr
Dim Arr As Variant
Dim Rng As Range
Dim FilePaths, FilePath
Dim FolderPath As String
Dim OpenWb As Workbook
Dim OpenSht As Worksheet Set Dic = CreateObject("Scripting.Dictionary")
Set Wb = Application.ThisWorkbook
FolderPath = Wb.Path & "\" & FOLDER_NAME & "\" Dim SheetName, RngAddress
Dim Areas, OneArea
Areas = Split(Setting, ";")
For Each OneArea In Areas
SheetName = Split(OneArea, "/")(0)
RngAddress = Split(OneArea, "/")(1)
'解析地址 初始化数组
On Error Resume Next
Set Sht = Wb.Worksheets(SheetName)
If Err.Number = 9 Then
MsgBox "当前工作簿不存在名为【" & SheetName & "】的工作簿!", vbInformation, "Information"
GoTo ErrorExit
End If
On Error GoTo 0 Set Rng = Sht.Range(RngAddress)
Rng.ClearContents
Arr = Rng.Value
Debug.Print SheetName; " "; RngAddress
Do
If Dic.Exists(SheetName) = False Then Exit Do
SheetName = SheetName & "@"
Loop
Dic(SheetName) = Array(RngAddress, Arr) Next OneArea FilePaths = FsoGetFiles(FolderPath, "*.xls*")
If FilePaths(1) = "None" Then
MsgBox "指定文件夹未找到任何工作簿!", vbInformation, "Information"
GoTo ErrorExit
End If For Each FilePath In FilePaths
Set OpenWb = Application.Workbooks.Open(FilePath)
For Each OneKey In Dic.Keys
SheetName = Replace(OneKey, "@", "")
On Error Resume Next
Set OpenSht = OpenWb.Worksheets(SheetName)
If Err.Number = 9 Then
MsgBox "打开工作簿不存在名为【" & SheetName & "】的工作簿!", vbInformation, "Information"
OpenWb.Close False
GoTo ErrorExit
End If
On Error GoTo 0 Ar = Dic(OneKey)
RngAddress = Ar(0)
Arr = Ar(1) Set Rng = OpenSht.Range(RngAddress)
Brr = Rng.Value For i = LBound(Arr) To UBound(Arr)
For j = LBound(Arr, 2) To UBound(Arr, 2)
If IsNumeric(Brr(i, j)) Then
'只有为数字时才可以相加
Arr(i, j) = Arr(i, j) + Brr(i, j)
Else
MsgBox "工作簿:" & FilePath & vbCr & _
"工作表:" & SheetName & vbCr & _
"单元格:" & Rng.Cells(i, j).Address & "的数据不是数字,不能累加"
GoTo ErrorExit
End If
Next j
Next i '更新求和数据
Ar(1) = Arr
Dic(OneKey) = Ar
Next OneKey
OpenWb.Close False
Next FilePath For Each OneKey In Dic.Keys
SheetName = Replace(OneKey, "@", "")
Ar = Dic(OneKey)
RngAddress = Ar(0)
Arr = Ar(1)
Set Sht = Wb.Worksheets(SheetName)
Set Rng = Sht.Range(RngAddress)
Rng.Value = Arr
Next OneKey UsedTime = VBA.Timer - StartTime
Debug.Print "UsedTime :" & Format(UsedTime, "#0.0000 Seconds")
'MsgBox "UsedTime :" & Format(UsedTime, "#0.0000 Seconds") ErrorExit:
Set Dic = Nothing
Set Wb = Nothing
Set Sht = Nothing
Set Rng = Nothing
Set OpenWb = Nothing
Set OpenSht = Nothing
Erase Arr
Erase Brr
Erase Ar
AppSettings False
End Sub
Function FsoGetFiles(ByVal FolderPath As String, ByVal Pattern As String, Optional ComplementPattern As String = "") As String()
Dim Arr() As String
Dim FSO As Object
Dim ThisFolder As Object
Dim OneFile As Object
ReDim Arr(1 To 1)
Arr(1) = "None"
Dim Index As Long
Index = 0
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error GoTo ErrorExit
Set ThisFolder = FSO.getfolder(FolderPath)
If Err.Number <> 0 Then Exit Function
For Each OneFile In ThisFolder.Files
If OneFile.Name Like Pattern Then
If Len(ComplementPattern) > 0 Then
If Not OneFile.Name Like ComplementPattern Then
Index = Index + 1
ReDim Preserve Arr(1 To Index)
Arr(Index) = OneFile.Path
End If
Else
Index = Index + 1
ReDim Preserve Arr(1 To Index)
Arr(Index) = OneFile.Path
End If
End If
Next OneFile
ErrorExit:
FsoGetFiles = Arr
Erase Arr
Set FSO = Nothing
Set ThisFolder = Nothing
Set OneFile = Nothing
End Function
Sub AppSettings(Optional IsStart As Boolean = True)
Application.ScreenUpdating = IIf(IsStart, False, True)
Application.DisplayAlerts = IIf(IsStart, False, True)
Application.Calculation = IIf(IsStart, xlCalculationManual, xlCalculationAutomatic)
Application.StatusBar = IIf(IsStart, ">>>>>>>>Macro Is Running>>>>>>>>", False)
End Sub
20180830xlVBA_合并计算的更多相关文章
- 20161227xlVBA多文件合并计算
Sub NextSeven_CodeFrame() '应用程序设置 Application.ScreenUpdating = False Application.DisplayAlerts = Fal ...
- Excel合并计算
office版本为2013,数据来源:我要自学网,曾贤志老师 计算之前,光标定在空白位置,不要定在数据源. 将汇总的类型居于首列(不可以跨区域选择,可以把不需要汇总的移动到其他列). 要有删除原来数据 ...
- iOS tableView 数据处理,数据分类相同数据整合、合并计算总数总价
// 数据下载得到数组数据 modelArray = [MZPriceModel mj_objectArrayWithKeyValuesArray:data[@"info"]]; ...
- Java的几个同步辅助类
Java为我们提供了一些同步辅助类,利用这些辅助类我们可以在多线程编程中,灵活地把握线程的状态. CountDownLatch CountDownLatch一个同步辅助类,在完成一组正在其他线程中执行 ...
- CSS3与页面布局学习总结(二)——Box Model、边距折叠、内联与块标签、CSSReset
一.盒子模型(Box Model) 盒子模型也有人称为框模型,HTML中的多数元素都会在浏览器中生成一个矩形的区域,每个区域包含四个组成部分,从外向内依次是:外边距(Margin).边框(Border ...
- java多线程--同步屏障CyclicBarrier的使用
CyclicBarrier的概念理解: CyclicBarrier的字面上的意思是可循环的屏障,是java并发包java.util.concurrent 里的一个同步工具类,在我下载的JDK1.6的中 ...
- 【分布式】Zookeeper应用场景
一.前言 在上一篇博客已经介绍了Zookeeper开源客户端的简单实用,本篇讲解Zookeeper的应用场景. 二.典型应用场景 Zookeeper是一个高可用的分布式数据管理和协调框架,并且能够很好 ...
- 【干货分享】前端面试知识点锦集02(CSS篇)——附答案
二.CSS部分 1.解释一下CSS的盒子模型? 回答一:a.标准的css盒子模型:宽度=内容的宽度+边框的宽度+加上内边具的宽度b.网页设计中常听的属性名:内容(content).填充(padding ...
- 探索C#之微型MapReduce
MapReduce近几年比较热的分布式计算编程模型,以C#为例简单介绍下MapReduce分布式计算. 阅读目录 背景 Map实现 Reduce实现 支持分布式 总结 背景 某平行世界程序猿小张接到B ...
随机推荐
- oracle数据库数据库表空间查询及扩充
1.查询表空间,及表空间的大小 SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size FROM dba_tabl ...
- Django组件(五) Django之ContentType组件
基础使用 -contenttype组件 -django提供的一个快速连表操作的组件,可以追踪项目中所有的APP和model的对应关系,并记录在ContentType表中. 当我们的项目做数据迁移后,会 ...
- 从0开始安装fedora23的笔记-- 以及使用fedora的常规问题-2
在shell中, 你是可以连续输入多个语句的, 中间用分号; 连接 也可以把这些多个语句放到一个函数中, 函数的话,便于多次引用. 而且 "封装" 为函数后, 可以用set查看到这 ...
- 【转载】EclEmma工具介绍
https://wenku.baidu.com/view/1017567e5acfa1c7aa00cc5f.html https://www.ibm.com/developerworks/cn/rat ...
- Excel 导出通用类
public class ExportToExcelHelper { public static void ExportExcel(DataTable dt) { try { //创建一个工作簿 IW ...
- CSS--点击改变样式
当某个链接或元素被选中时可以时,需要改变其颜色或状态,而stylus中提供&选择器,&指向父选择器,用于判断父元素达到某条件时改变状态,下面的例子中当父元素router-link有被选 ...
- hdu 5724 Chess 博弈sg+状态压缩
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem De ...
- PHP里获取一维数组里的最大值和最小值
<?php $arr = ['10','100','50','90','2','5']; $min = min($arr); $max = max($arr); echo $min.PHP_EO ...
- NoSQL(not only struts query language)的简单介绍
为什么需要NoSQL? 互联网自扩大规模来一直面临3个问题 1.High performance高并发 一个网站开发实时生成动态页面可能会存在高并发请求的需求,硬盘IO已经无法接受 2.Huge St ...
- 导入dmp文件时,需要删除原有ORACLE数据库实例
导入dmp文件时,对于已存在的数据库实例及表处理方式:删除实例. 1.以管理员身份登录 sqlplus / as sysdba 2.停止实例 shutdown abort; 执行结果:ORACLE i ...