VBA excel 打开本路径下的A.Xls文件,并把文件中的所有工作表的明细数据合并到本表中,上下排列

Sub 选择粘贴()
Dim ro As Integer, i As Integer, col As Integer
For i = 2 To Sheets.Count
ro = Sheets(i).Range("a666").End(xlUp).Row
col = Sheets(i).Range("H1").End(xlLeft).Column
Sheets(i).Range(Cells(2, 1), Cells(ro, col)).Copy Sheets(1).Range("a6666").End(xlUp).Offset(-1, 0)
Next i
End Sub
这个程序提示 应用程序定义或对象定义出错
求大神 拯救

亲,代码如下。

假设你的文件在“d:\test\A.xls”。

Sub hz()
Dim bt, i, r, c, n, first As Long
Dim f
Set f = Workbooks.Open("d:\test\A.xls")
bt = 1 '表头有几行,这里的1就改成几
ThisWorkbook.ActiveSheet.Cells.Clear
For i = 1 To f.Sheets.Count
    If first = 0 Then
        c = f.Sheets(i).Cells(1, Columns.Count).End(xlToLeft).Column
        f.Sheets(i).Range("A1").Resize(bt, c).Copy ThisWorkbook.ActiveSheet.Range("A1")
        n = bt + 1: first = 1
    End If
    r = f.Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row
    f.Sheets(i).Range("A" & bt + 1).Resize(r - bt, c).Copy ThisWorkbook.ActiveSheet.Range("A" & n)
    n = n + r - bt
Next
f.Close False
End Sub

追问

大神 我只是不明白 我的程序为啥实现不了

追答

col = Sheets(i).Range("H1").End(xlLeft).Column,这一句,其中的xlleft应该是xltoleft

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答