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的更多相关文章
随机推荐
- Python3 tkinter基础 Canvas bind 鼠标左键点击时,在当前位置画椭圆形
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 浅尝flutter中的http请求
import 'package:flutter/material.dart'; class News extends StatefulWidget { final String title,imgli ...
- 【做题】NOWCODER142A Ternary String——数列&欧拉定理
题意:你有一个长度为\(n\),且仅由012构成的字符串.每经过一秒,这个字符串所有1后面会插入一个0,所有2后面会插入一个1,然后会删除第一个元素.求这个字符串需要多少秒变为空串,对\(10^9+7 ...
- Dubbo集群配置和官方文档
集群配置: https://blog.csdn.net/zh520qx/article/details/63679908 https://www.cnblogs.com/hd3013779515/p/ ...
- (转)开源项目miaosha(下)
石墨文档:https://shimo.im/docs/2XlwliBQAYsKCHbq/ (二期)20.开源秒杀项目miaosha解读(下) [课程20]jmeter.xmind81.5KB [课程2 ...
- 在VMware14上安装centos6.5
打开vmware14 => 创建新虚拟机(即再建一个linux),已有光盘映像文件,正常操作即可.
- C# winfrom 当前程序内存读取和控制
https://zhidao.baidu.com/question/31914620.html https://www.cnblogs.com/xcsn/p/4678322.html Process ...
- TIM定时器的应用
TIM定时器的应用 ①输入捕获的应用: 上一节,我已阐述TIM的输入捕获具体作用有两个(如下图): 对输入信号的测量: ...
- DataTableHelper
public class DataTableHelper { /// <summary> /// 给DataTable增加一个自增列 /// 如果DataTable 存在 identity ...
- 每日质量NPM包事件绑定_bindme(详解React的this)
一.bindme 官方定义: is a helper to bind a list of methods to an object reference 理解: 因为不推荐在render()里构建函数, ...