|
各位大神:小弟每个月会统计各部门的一些数据,他们反馈过来的表格要合在一张表里。表格有一行表头,共31列,总行数不固定,求教CBA代码应该怎么写呢?下方的这段代码是从《别怕,VBA其实很简单里面复制出来的》,初学还不太懂,求大神帮忙改下,谢谢~
Sub 表格合并()
Dim bt As Range, r As Long, c As Long
r = 1
c = 31
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")
Do While filename <> ""
If filename <> ThisWorkbook.Name Then
erow = wt.Range("A1").CurrentRegion.Rows.Count + 1
fn = ThisWorkbook.Path & "\" & filename
Set wb = GetObject(fn)
Set sht = wb.Worksheets(1)
arr = sht.Range(sht.Cells(r + 1, "A"), sht.Cells(1048576, "B").End(xlUp).Offset(0, 5))
wt.Cells(erow, "A").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
wb.Close False
End If
filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
|
|