20180831xlVBA_WorksheetsCosolidate
Sub WorkSheetsConsolidate()
Rem 设置求和区域为 单元格区域;单元格区域
Const Setting As String = "A1;B2:C4"
Dim StartTime As Variant
Dim UsedTime As Variant
StartTime = VBA.Timer AppSettings True
Dim Wb As Workbook
Dim Sht As Worksheet
Dim OneSht As Worksheet
Const MAIN_SHEET As String = "1"
Dim Dic As Object
Dim Key As String
Dim OneKey
Dim Brr
Dim Arr As Variant
Dim Rng As Range
Dim RngAddress
Dim Areas, OneArea Set Dic = CreateObject("Scripting.Dictionary")
Set Wb = Application.ThisWorkbook
Set Sht = Wb.Worksheets(MAIN_SHEET) Areas = Split(Setting, ";")
For Each OneArea In Areas
RngAddress = OneArea
Set Rng = Sht.Range(RngAddress)
Rng.ClearContents
Arr = Rng.Value
Dic(RngAddress) = Arr
Next OneArea For Each OneKey In Dic.Keys
For Each OneSht In Wb.Worksheets
If OneSht.Name <> Sht.Name Then
Arr = Dic(OneKey)
RngAddress = OneKey
Set Rng = OneSht.Range(RngAddress)
Brr = Rng.Value If Rng.Cells.Count > 1 Then 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 "工作表:" & OneSht.Name & vbCr & _
"单元格:" & Rng.Cells(i, j).Address & "的数据不是数字,不能累加"
GoTo ErrorExit
End If
Next j
Next i
Else
Arr = Arr + Brr
End If
'更新求和数据
Dic(OneKey) = Arr
End If
Next OneSht
Next OneKey For Each OneKey In Dic.Keys
RngAddress = OneKey
Arr = Dic(OneKey)
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 AppSettings False
End Sub 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
20180831xlVBA_WorksheetsCosolidate的更多相关文章
随机推荐
- 一些常用的mysql语句实例-以后照写
create database blog; create table blog_user ( user_Name char(15) not null check(user_Name !=''), us ...
- SP10707 COT2 - Count on a tree II 莫队
链接 https://vjudge.net/problem/SPOJ-COT2 https://www.luogu.org/problemnew/show/SP10707 思路 dfs欧拉序转化为普通 ...
- Java8 函数式接口-Functional Interface
目录 函数式接口: JDK 8之前已有的函数式接口: 新定义的函数式接口: 函数式接口中可以额外定义多个Object的public方法一样抽象方法: 声明异常: 静态方法: 默认方法 泛型及继承关系 ...
- 大臣的旅费|2013年蓝桥杯A组题解析第十题-fishers
标题:大臣的旅费 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市 ...
- 【索引失效】什么情况下会引起MySQL索引失效
索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因) 注意:要想使用or,又想让索引生效,只能将or条件 ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
本文为博主原创: 以下为在程序运行过程中报的错误, org.springframework.dao.CannotAcquireLockException: ### Error updating dat ...
- 良好的GUI设计指南
一.设计指南 摘自:<需求分析与系统设计(第3版)> 7.1.2. 1. 用户控制 用户事件(菜单动作.鼠标点击.屏幕光标移动等)打开GUI窗口或调用程序:程序执行需要反馈到用户. 2. ...
- Git stash 常用命令
参考: Git: How to look at the stash Git学习笔记05--git stash Git stash 常用命令 1.git stash: 保存当前的工作进度: 2.git ...
- snakemake使用笔记
snakemake是一个用来编写任务流程的工具,用python编写的,因此其执行的流程脚本也比较通俗易懂,易于理解. 一.从一个简单的例子开始 1.安装snakemake 安装snakemake的方法 ...
- 前端性能优化之按需加载(React-router+webpack)
一.什么是按需加载 和异步加载script的目的一样(异步加载script的方法),按需加载/代码切割也可以解决首屏加载的速度. 什么时候需要按需加载 如果是大文件,使用按需加载就十分合适.比如一个近 ...