1

I have assembled this code together to copy different ranges from multiple sheets to master sheet. However for copyRng 7, instead of going underneath the copyrng6, it overwrites copyrng6.

Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng1 As Range
Dim CopyRng2 As Range
Dim CopyRng3 As Range
Dim CopyRng4 As Range
Dim CopyRng5 As Range
Dim CopyRng6 As Range
Dim CopyRng7 As Range
Dim cell As Range
Dim Row As Range



With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With


Application.DisplayAlerts = False

Set DestSh = Sheets("Main")


'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
    If sh.Name <> DestSh.Name And sh.Name <> "Main" And sh.Name <> "Master" Then

        'Find the last row with data on the DestSh
        Last = LastRow(DestSh)

        'Fill in the range that you want to copy
        Set CopyRng1 = sh.Range("B3")
        Set CopyRng2 = sh.Range("C3")
        Set CopyRng3 = sh.Range("D3")
        Set CopyRng4 = sh.Range("G3")
        Set CopyRng5 = sh.Range("C5")
        Set CopyRng6 = sh.Range("A8:j25")
        Set CopyRng7 = sh.Range("A28:j44")

        'Test if there enough rows in the DestSh to copy all the data
        If Last + CopyRng1.Rows.Count > DestSh.Rows.Count Then
            MsgBox "There are not enough rows in the Destsh"
            GoTo ExitTheSub
        End If

        'This example copies values/formats, if you only want to copy the
        'values or want to copy everything look at the example below this macro
        CopyRng1.Copy
        With DestSh.Cells(Last + 1, "A")
            .PasteSpecial xlPasteValues
            Application.CutCopyMode = False
        End With
        CopyRng2.Copy
        With DestSh.Cells(Last + 1, "B")
            .PasteSpecial xlPasteValues
            Application.CutCopyMode = False
        End With
        CopyRng3.Copy
        With DestSh.Cells(Last + 1, "C")
            .PasteSpecial xlPasteValues
            Application.CutCopyMode = False
        End With

         CopyRng4.Copy
        With DestSh.Cells(Last + 1, "D")
            .PasteSpecial xlPasteValues
            Application.CutCopyMode = False
        End With
         CopyRng5.Copy
        With DestSh.Cells(Last + 1, "E")
            .PasteSpecial xlPasteValues
            Application.CutCopyMode = False
        End With


        CopyRng6.Copy
        With DestSh.Cells(Last + 1, "F")
            .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
            Application.CutCopyMode = False
        End With
         CopyRng7.Copy
        With DestSh.Cells(Last + 1, "F")
            .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
            Application.CutCopyMode = False
        End With


    End If
Next

 ExitTheSub:

Application.Goto DestSh.Cells(1)

'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
                        After:=sh.Range("A1"), _
                        Lookat:=xlPart, _
                        LookIn:=xlFormulas, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Row
On Error GoTo 0
End Function

Master Sheet

Thanks in advance. This is my first question I do apologize in advance for any mistake or confusion. I can offer more explanation if asked. Thanks

4
  • Hello Scott, I have changed it to 1 question, thanks. It was first time so i apologize. Commented Aug 1, 2018 at 19:58
  • 1
    after both CopyRng6 and CopyRng7 you have the same line: With DestSh.Cells(Last + 1, "F"), so of course 7 will overwrite 6 :) Commented Aug 1, 2018 at 20:00
  • I want to copy copyrng7 to go in F as well for the destsh, however once copyrng6 is pasted and underneath copyrng6 Commented Aug 1, 2018 at 20:02
  • 1
    then you have to refresh the last variable to get the new last row Commented Aug 1, 2018 at 20:03

1 Answer 1

1

refresh the last variable between 6 and 7 copy to refresh the new last row on the sheet after 6 is copied:

    CopyRng6.Copy
    With DestSh.Cells(Last + 1, "F")
        .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
        Application.CutCopyMode = False
    End With

    last = LastRow(DestSh)

     CopyRng7.Copy
    With DestSh.Cells(Last + 1, "F")
        .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
        Application.CutCopyMode = False
    End With
Sign up to request clarification or add additional context in comments.

3 Comments

@Navkaur great. please mark as answered so others get the benefit moving forward.
how to mark the question as answered?by select the tick mark?
Hello Scott can you please have a look at my other question as well, I will really appreciate it. stackoverflow.com/questions/51642488/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.