'2017年11月13日
'Next_Seven
'功能:文件夹对话框指定文件夹下,合并(复制粘贴)每个Excel文件内的指定子表内容,
'在名为"设置"的工作表A列 输入汇总子表的名称 在B列输入汇总子表的表头行数
'C列自动输出 有效汇总的sheet个数
Public Sub 指定文件夹多簿多表分表合并()
AppSettings True
Dim StartTime As Variant
Dim UsedTime As Variant
StartTime = VBA.Timer Dim FolderPath As String, FileName As String, FilePath As String
Dim Arr As Variant, dSht As Object, Sht As Worksheet, Wb As Workbook
Dim EndRow As Long, EndCol As Long, Ar As Variant
Dim i As Long, j As Long, HeadRow As Long, NextRow As Long
Dim Key As String, NewSht As Worksheet, Rng As Range
Dim OpenWb As Workbook, OpenSht As Worksheet Set dSht = CreateObject("Scripting.Dictionary")
Set Wb = Application.ThisWorkbook
Set Sht = Wb.Worksheets("设置")
With Sht
Application.Intersect(.Range("C:C"), .UsedRange.Offset(1)).ClearContents
EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
If EndRow <= 1 Then
MsgBox "未设置工作表名称!", vbInformation, "AuthorQQ 84857038"
Exit Sub
End If
For i = 2 To EndRow
If Len(.Cells(i, 2).Value) = 0 Then
HeadRow = 1
Else
HeadRow = .Cells(i, 2).Value
End If
Key = Trim(.Cells(i, 1).Text)
dSht(Key) = Array(Key, HeadRow, 0)
Next i
End With '获取文件夹路径
FolderPath = GetFolderPath(ThisWorkbook.Path)
If Len(FolderPath) = 0 Then
MsgBox "您没有选中任何文件夹,本次汇总中断!"
Exit Sub
End If '获取文件名列表
Arr = FsoGetFiles(FolderPath, "*.xls*", "*" & ThisWorkbook.Name & "*")
For i = LBound(Arr) To UBound(Arr)
FilePath = CStr(Arr(i))
Debug.Print FilePath Set OpenWb = Application.Workbooks.Open(FilePath)
For Each OpenSht In OpenWb.Worksheets
Key = OpenSht.Name
If dSht.Exists(Key) Then
Ar = dSht(Key)
HeadRow = Ar(1)
If Ar(2) = 0 Then
'创建新工作表
Set NewSht = AddWorksheet(Wb, Key, True)
If Application.WorksheetFunction.CountA(OpenSht.Cells) > 0 Then
OpenSht.UsedRange.Copy NewSht.Range("A1")
Ar(2) = Ar(2) + 1
End If
Else
Set NewSht = Wb.Worksheets(Key)
If Application.WorksheetFunction.CountA(OpenSht.Cells) > 0 Then
With NewSht
NextRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row + 1
OpenSht.UsedRange.Offset(HeadRow).Copy .Cells(NextRow, 1)
End With
Ar(2) = Ar(2) + 1
End If
End If dSht(Key) = Ar End If
Next OpenSht
OpenWb.Close False Next i With Sht
Set Rng = .Range("A2")
Set Rng = Rng.Resize(dSht.Count, 3)
Rng.Value = Application.Rept(dSht.Items, 1)
End With Set dSht = Nothing
Set Sht = Nothing
Set NewSht = Nothing
Set OpenWb = Nothing
Set OpenSht = Nothing
Set Rng = Nothing UsedTime = VBA.Timer - StartTime
Debug.Print "UsedTime :" & Format(UsedTime, "#0.0000 Seconds")
'MsgBox "UsedTime :" & Format(UsedTime, "#0.0000 Seconds")
AppSettings False End Sub Private Function GetFolderPath(InitialPath) As String
Dim FolderPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = InitialPath
.AllowMultiSelect = False
.Title = "请选取Excel工作簿所在文件夹"
If .Show = -1 Then
FolderPath = .SelectedItems(1)
Else
GetFolderPath = ""
'MsgBox "您没有选中任何文件夹,本次汇总中断!"
Exit Function
End If
End With If Right(FolderPath, 1) <> Application.PathSeparator Then FolderPath = FolderPath & Application.PathSeparator
GetFolderPath = FolderPath
End Function
Private 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
Private Function AddWorksheet(ByVal Wb As Workbook, ByVal ShtName As String, Optional ReplaceSymbol As Boolean = True) As Worksheet
Dim Sht As Worksheet
If Len(ShtName) = 0 Or Len(ShtName) > 31 Then
Set AddWorksheet = Nothing
MsgBox "Worksheet名称长度不符!", vbInformation, "AddWorksheet"
Exit Function
Else
On Error Resume Next
Set Sht = Wb.Worksheets(ShtName)
If Err.Number = 9 Then
Set Sht = Wb.Worksheets.Add(After:=Wb.Worksheets(Wb.Worksheets.Count))
Err.Clear
On Error GoTo 0
On Error Resume Next
Sht.Name = ShtName
If Err.Number = 1004 Then
Err.Clear
On Error GoTo 0
If ReplaceSymbol Then
Arr = Array("/", "\", "?", "*", "[", "]")
For i = LBound(Arr) To UBound(Arr)
ShtName = Replace(ShtName, Arr(i), "")
Next i
Set AddWorksheet = AddWorksheet(Wb, ShtName) '再次调用
Else
Set AddWorksheet = Nothing
MsgBox "Worksheet名称含有特殊符号!", vbInformation, "AddWorksheet"
End If
Else
Set AddWorksheet = Sht
End If
ElseIf Err.Number = 0 Then
Set AddWorksheet = Sht
End If
End If
End Function
Public 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

  

20171113xlVba指定文件夹多簿多表分表合并150的更多相关文章

  1. summernote图片上传功能保存到服务器指定文件夹+php代码+java方法

    1.summernote富文本编辑器 summernote是一款基于bootstrap的富文本编辑器,是一款十分好用的文本编辑器,还附带有图片和文件上传功能. 那么在我们网站中想吧这个图片上传到服务器 ...

  2. JAVA 批量下载服务器文件到本地指定文件夹并重命名

    /** * @功能 下载文件到指定文件夹并重命名 * @param url 请求的路径 * @param filePath 文件将要保存的目录 * @param filename 保存到本地的文件名 ...

  3. 怎么统计指定文件夹下含有.xml格式的文件数目

    如何统计指定文件夹下含有.xml格式的文件数目?如题 ------解决思路----------------------Directory.GetFiles(@"路径", " ...

  4. PHP批量清空删除指定文件夹内容

    PHP批量清空删除指定文件夹内容: cleancache.php <?php // 清文件缓存 $dirs = array( realpath(dirname(__FILE__) . '/../ ...

  5. C#实现把指定文件夹下的所有文件复制到指定路径下以及修改指定文件的后缀名

    1.实现把指定文件夹下的所有文件复制到指定路径下 public static void copyFiles(string path) { DirectoryInfo dir = new Directo ...

  6. [转]C#中调用资源管理器(Explorer.exe)打开指定文件夹 + 并选中指定文件 + 调用(系统默认的播放类)软件(如WMP)打开(播放歌曲等)文件

    原文:http://www.crifan.com/csharp_call_explorer_to_open_destinate_folder_and_select_specific_file/ C#中 ...

  7. (Python)导出指定文件夹中as文件的完全限定类名

    AS3程序在编译的过程中,有一个特点是这样的,不管是项目中的类,还是标准库或者第三方库的类,编译的时候只会把用到的那些类文件编译进去,也就是说,某一些类,只要没有被主程序引用到,那这个文件是不会被编译 ...

  8. 将java的class文件放到一个指定文件夹下

    用javac执行java文件时,要把java文件的class文件放到指定文件夹下,注意文件夹要创建好,执行javac -d 文件夹 ***.java 如图: 在class文件夹下就出现了L的class ...

  9. Android 遍历sdcard中指定文件夹下的图片(jpg,jpeg,png)

    File scanner5Directory = new File(Environment.getExternalStorageDirectory().getPath() + "/scann ...

随机推荐

  1. Selenium android driver

    selenium android « s « Jar File Download http://www.java2s.com/Code/Jar/s/selenium-android.htm How t ...

  2. topcoder srm 708 div1 -3

    1.定义一个字符串s,定义函数$f(s)=\sum_{i=1}^{i<|s|}[s_{i-1}\neq s_{i}]$,给定字符串$p,q$,定义函数$g(p,q)=\sum_{c='a'}^{ ...

  3. 为什么不能用drop function add 去删除函数? 因为不能使用 mysql中的保留字!

    mysql中有很多的 保留字, 也叫关键字, 你在使用 数据库中的任何东西, 都最好是 避开这些关键字/保留字, 包括 数据库名, 表名, 字段名, 函数名, 存储过程名. 这些关键字包括: mysq ...

  4. Hadoop【单机安装-测试程序WordCount】

    Hadoop程序说明,就是创建一个文本文件,然后统计这个文本文件中单词出现过多少次! (MapReduce 运行在本地   启动JVM ) 第一步    创建需要的文件目录,然后进入该文件中进行编辑 ...

  5. 【OData】使用Odata获取数据之后再次获取可能得不到最新的数据问题记录

    工作上遇到个问题是关于系统后台数据库更新了某数据后, 前台界面刷新显示的不是最新的数据.但是大约10分后再次刷新就能显示新的数据,或者重启IIS等web server host. 最开始认为可能是因为 ...

  6. Paper Reviews and Presentations

    Copied from Advanced Computer Networks, Johns Hopkins University. Paper Reviews and Presentations Ea ...

  7. SAP 供应商/客户的冻结及其删除操作

    SAP 供应商/客户的冻结及其删除操作 在SAP中,有所谓的财务供应商(Tcode:FK01)和后勤供应商(Tcode:XK01),供应商和客户主数据包括一般数据/公司代码数据/采购组织|销售范围三方 ...

  8. 网页中Div的打印

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. 2018.3 江苏省计算机等级考试 C语言 编程题答案

    题目要求:给定一个数字范围,输出满足这些条件: 1.能被3整除: 2.包含数字5, 将满足的数字放在特定的数组里输出.输出这些数里5出现的个数.数字的个数. 想起来有点伤心,本来很简单的题,考试的时候 ...

  10. curl java 模拟http请求

    curl java 模拟http请求 直接上代码: public static void main(String args[]) throws Exception { String url = &qu ...