|
Sub 合同同一文件夹下的所有表()
dim r as long
r = 1
Application.ScreenUpdating = False
Dim filename As String, wb As Workbook, sht As Worksheet, erow As Long, fn As String, arr As Variant, flag As Integer
filename = Dir(ThisWorkbook.Path & "\*.xls")
flag = 1
Do While filename <> ""
If filename <> ThisWorkbook.Name Then
If flag = 1 Then
erow = 1
Else
erow = Range("A1").CurrentRegion.Rows.Count + 1
End If
fn = ThisWorkbook.Path & "\" & filename
Set wb = GetObject(fn)
Set sht = wb.Worksheets(1)
If flag = 1 Then
arr = sht.Range(sht.Cells(r, "A"), sht.Cells(65536, "B").End(xlUp).Offset(0, 9))
Else
arr = sht.Range(sht.Cells(r + 1, "A"), sht.Cells(65536, "B").End(xlUp).Offset(0, 9))
End If
Cells(erow, "A").Resize(UBound(arr, 1), UBound(arr, 2)) = arr 'UBound(arr, 1)计算出行数,UBound(arr, 2)计算出列数
wb.Close False
End If
filename = Dir
flag = 2
Loop
Application.ScreenUpdating = True
End Sub
这个代码为了合并当前目录下所有的excel文档,但是有个问题是合并完了以后第一列的标题行没有了,在flag=1的时候,即第一个文档时候保留了标题的,为什么后面不见了?有老师解答一下吗?需要怎么改能保留标题行?
|
|