0

I need a code to copy everything from various tabs in cells A:H (starting in row 3) and paste everything on the main tab starting in cell B5 and moving down?

My current code is:

Sub CopyToMainsheet()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Main" Then
            ws.Activate
            Range("A3:H3").Select
            Range(Selection, Selection.End(xlDown)).Copy
            Sheets("Main").Select
            Range("b" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
        End If
    Next
End Sub

The issue with this code is that it doesn't go back to B5 if you do it more than once and keeps pasting below what has already been pasted. I need it to start pasting in B5 every time.

Thanks in advance

Robert Todar
  • 2,085
  • 2
  • 11
  • 31
  • Why not just clear your output sheet at the start of the code? – SJR Jul 08 '19 at 16:19
  • 1
    Avoid using `Select`. And make the `Worksheet` the `Range`s are on explicit. See [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). – BigBen Jul 08 '19 at 16:22
  • @SJR Because it should be in a table format... if that makes sense? – LondonLondon Jul 08 '19 at 16:35

1 Answers1

1

Try this. If there's stuff in Main you want to preserve, that bit will need tweaking.

Sub CopyToMainsheet()

Dim ws As Worksheet, r As Long, r1 As Long

r = 5
With Worksheets("Main")
    r1 = .Range("B" & Rows.Count).End(xlUp).Row
    If r1 > 4 Then .Range("B5", .Range("B" & Rows.Count).End(xlUp)).Resize(, 8).ClearContents
End With

For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Main" Then
        ws.Range("A3", ws.Range("A" & Rows.Count).End(xlUp)).Resize(, 8).Copy
        Sheets("Main").Range("B" & r).PasteSpecial Paste:=xlPasteValues, _
                                                   Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        r = Sheets("Main").Range("B" & Rows.Count).End(xlUp).Row + 1
    End If
Next

End Sub
SJR
  • 22,986
  • 6
  • 18
  • 26