20180831xlVBA_WorkbooksCosolidate
Sub WorkbooksConsolidate()
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 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 "工作簿:" & FilePath & vbCr & _
"工作表:" & SheetName & vbCr & _
"单元格:" & Rng.Cells(i, j).Address & "的数据不是数字,不能累加"
GoTo ErrorExit
End If
Next j
Next i Else
If IsNumeric(Brr) Then
'只有为数字时才可以相加
Arr = Arr + Brr
Else
MsgBox "工作簿:" & FilePath & vbCr & _
"工作表:" & SheetName & vbCr & _
"单元格:" & Rng.Cells(i, j).Address & "的数据不是数字,不能累加"
GoTo ErrorExit
End If
End If '更新求和数据
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
20180831xlVBA_WorkbooksCosolidate的更多相关文章
随机推荐
- phpMyAdmin - Error
centos7.5 原因:权限问题 解决方法:授权 [root@web01 code]# groupadd -g666 www [root@web01 code]# useradd -u666 -g6 ...
- chrome浏览器中的百度搜索引擎总是先自动跳转到百度首页怎么办?
1.修改百度搜索引擎代码如下: http://www.baidu.com/s?wd=%s&ie={inputEncoding} 备注:现在chorme不能直接在默认的百度搜索引擎上修改,而且自 ...
- Docker 入门指南——资源工具篇
好工具 dive wagoodman/dive A tool for exploring each layer in a docker image 参考 用 Dive 看 Docker Image 裡 ...
- Python SSH爆破以及Python3线程池控制线程数
源自一个朋友的要求,他的要求是只爆破一个ip,结果出来后就停止,如果是爆破多个,完全没必要停止,等他跑完就好 #!usr/bin/env python #!coding=utf-8 __author_ ...
- 51nod 四级题 汇总
51Nod-1060-最复杂的数 #include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; ...
- 【C#】取整函数Math.Round、Math.Ceiling和Math.Floor区别
Math.Round 原则: 四舍六入五取偶. 也就是说 0.1-0.4为0 0.5为0 0.6-0.9为1 1.5为2 Math.Ceiling 原则: 有小数就加1 0.1 = 1 Math.Fl ...
- java.lang.NoClassDefFoundError: com/gexin/rp/sdk/exceptions/RequestException解决方法
本文为博主原创,未经允许不得转载: 最近在开发个推的时候遇到的问题,当我在maven仓库中下载个推的jar包时,下载不下来,索性在项目中Configue build Path,将jar下载到本地 手动 ...
- 1、Python中的正则表达式(0601)
回顾: 1.文件对象: open('file','mode','bufsize') read,readline,readlines,write,writelines,flush,seek,tell 2 ...
- hihoCoder 1339 Dice Possibility(DP)
http://hihocoder.com/problemset/problem/1339 题意: 求一个骰子扔n次后最后点数相加为m的概率. 思路: f[i][j]表示扔到第i次时总值为j的概率. # ...
- 【Web Service】
Restful: (Representational State Transfer 表现层[指客户端]状态[指服务器端]转化) RPC: RPC 风格的开发关注于服务器/客户端之间的方法调用, 而并 ...