Copy data from multiple sheets and paste into a single one

gimbox16

New Member
Joined
Oct 21, 2020
Messages
1
Office Version
  1. 365
Platform
  1. MacOS
Hello,
I'm very new to VBA and I've seen that there are many similar questions to mine, but none fits exactly my needs.
I have a workbook with several sheets; my main sheet "Final" has in column A all the names of all the other sheets (e.g. 18MP2, 23ST4, etc). The sheets with such names all have the same structure. I would like to copy from each of these sheets the range A2:G2 and paste it in the sheet "Final" in the range B:G according to the match between the value in col A (of sheet "Final") and the name of the sheet from which the data is copied. Below you can see a pic of how the workbook looks.

I'd appreciate any help!


Screenshot 2020-10-21 at 11.34.48.png
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If you are copying A:G then the paste rand would be B:H?

I used the sheet named "Final" as shown in your description
We should also make sure the sheet exists.

VBA Code:
Sub Get_R()
    Dim sh As Worksheet
    Dim rng As Range, c As Range

    Set sh = Sheets("Final")
    With sh
        Set rng = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For Each c In rng.Cells
            If SheetExists(c.Value) Then
                c.Offset(, 1).Resize(, 7) = Sheets(c.Value).Range("A2:G2").Value
            Else
                c.Offset(, 1) = c & " does not exist"
            End If
        Next c
    End With
End Sub

Function SheetExists(SheetName As String)
    On Error GoTo no:
    WorksheetName = Worksheets(SheetName).Name
    SheetExists = True
    Exit Function
no:
    SheetExists = False
End Function
 
Upvote 0
Hi,
What about
VBA Code:
Sub test()
Dim i
Dim a, b As Variant
    With Sheets("Final")
        a = .Cells(2, 1).Resize(.Cells(Rows.Count, 1).End(xlUp).Row - 1)
        ReDim b(1 To UBound(a))
        For i = 1 To UBound(a)
            With Sheets(a(i, 1))
                b(i) = .Cells(2, 1).Resize(, 7)
            End With
        Next
        .Cells(2, 2).Resize(UBound(a), 7) = Application.Transpose(Application.Transpose(b))
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top