vba-多个工作簿合并方法1(用到了数组)
Posted xlgCode
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vba-多个工作簿合并方法1(用到了数组)相关的知识,希望对你有一定的参考价值。
来自《别怕excel vba其实很简单》
Sub 多个工作簿合并()
‘
‘多个工作簿合并 宏
Dim bt As Range, r As Long, c As Long
r = 1
c = 7
Dim wt As Worksheet
Set wt = ThisWorkbook.Worksheets(1)
wt.Rows(r + 1 & ":1048576").ClearContents
Application.ScreenUpdating = False
Dim filename As String, sht As Worksheet, wb As Workbook
Dim erow As Long, fn As String, arr As Variant
filename = Dir(ThisWorkbook.Path & "\*.xlsx") ‘dir-获取文件名
‘MsgBox ("这个是:" & ThisWorkbook.Path)-->"这个是C;\...desktop\新建文件夹"
Do While filename <> ""
If filename <> ThisWorkbook.Name Then
erow = wt.Range("a1").CurrentRegion.Rows.Count + 1 ‘当前区域的行数+1-->获得汇总表第一行的行号?
fn = ThisWorkbook.Path & "\" & filename ‘将第1个要汇总的工作簿名称赋给变量fn
Set wb = GetObject(fn) ‘将变量fn代表的工作簿对象赋给wb
Set sht = wb.Worksheets(1) ‘将要汇总工作表赋给sht
arr = sht.Range(sht.Cells(1, "A"), sht.Cells(1048576, "b").End(xlUp).Offset(0, 5))
‘将工作表中要汇总的记录保存在数组arr中
wt.Cells(erow, 1).Resize(UBound(arr, 1), UBound(arr, 2)) = arr ‘?
wb.Close False
End If
filename = Dir ‘用dir函数取得其他文件名,并赋给变量??
Loop
Application.ScreenUpdating = True
End Sub
以上是关于vba-多个工作簿合并方法1(用到了数组)的主要内容,如果未能解决你的问题,请参考以下文章
excel或者vba,怎样将工作簿内所有橙色单元格公式转换为数值?
【OFFICE 365】Power Query 多工作簿合并