复制到另一个工作表时出错

时间:2018-04-27 12:55:46

标签: excel vba excel-vba

你好我有一个副本从wb中的每个工作表复制到一个名为"表"的工作表,工作表由多个表组成,我需要从顶部复制第一个表,没有每个表格的标题和汇总包含从表格到表格"表格":

Sub TableCopy()
Dim ws1  As Worksheet, _
LR1 As Long, _
LR2 As Long
Application.ScreenUpdating = False
For Each ws1 In ActiveWorkbook.Worksheets
If ws1.Name <> "Table" Then
    LR1 = Sheets("Table").Range("H" & Rows.Count).End(xlUp).Row + 1
    LR2 = ws1.Range("B8", Range("B8").End(xlDown)).Rows.Count - 1
    ws1.Range("A:S" & LR2).Copy Destination:=Sheets("Table").Range("A" & LR1)
End If
Next ws1
Application.ScreenUpdating = True
End Sub

我正在

  

&#39;对象的方法范围&#39;工作表失败

我无法掌握它,你能帮助我吗? THX

2 个答案:

答案 0 :(得分:0)

两个修正:

Sub TableCopy()
Dim ws1  As Worksheet, _
LR1 As Long, _
LR2 As Long
Application.ScreenUpdating = False
For Each ws1 In ActiveWorkbook.Worksheets
If ws1.Name <> "Table" Then
    LR1 = Sheets("Table").Range("H" & Sheets("Table").Rows.Count).End(xlUp).Row + 1 'Fix 1 (qualified the rows.count)
    LR2 = ws1.Range("B8", Range("B8").End(xlDown)).Rows.Count - 1
    ws1.Range("A2:S" & LR2).Copy Destination:=Sheets("Table").Range("A" & LR1) 'Fix 2 (added a # for starting row in copy range, assume row 1 is header)
End If
Next ws1
Application.ScreenUpdating = True
End Sub

答案 1 :(得分:0)

Sub TableCopy()
Dim ws1  As Worksheet, _
LR1 As Long, _
LR2 As Long
Application.ScreenUpdating = False

For Each ws1 In ActiveWorkbook.Worksheets
If ws1.Name <> "Table" Then
    LR1 = Worksheets("Table").Cells(Worksheets("Table").Rows.count, "H").End(xlUp).row + 1
    LR2 = ws1.Cells(ws1.Rows.count, "B").End(xlUp).row - 9
    ws1.Range("A2:S" & LR2).Copy Destination:=Worksheets("Table").Range("A" & LR1)

End If
Next ws1
Application.ScreenUpdating = True
End Sub