Copy data from selection of sheets

andysh

Board Regular
Joined
Nov 8, 2019
Messages
111
Hi

I'm trying to collate the data from sheets 2 through 11 onto sheet 1 of a workbook but I'm struggling with the macro.

How would I go about cutting and pasting the rows with data from sheets 2 to 11 (without headers) on to the next available row on sheet 1?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Have renamed Sheet1 as "Master". You can change this in the code if you desire.
Code:
Option Explicit


Sub cpypste()
    Dim ws As Worksheet, sh As Worksheet
    Set sh = Sheets("Master")
    Dim lr As Long, lrw As Long, lc As Long
    Application.ScreenUpdating = False
    For Each ws In Worksheets
        If ws.Name <> "Master" Then
            lrw = ws.Range("A" & Rows.Count).End(xlUp).Row
            lc = ws.Cells(1, Columns.Count).End(xlToLeft).Column
            lr = sh.Range("A" & Rows.Count).End(xlUp).Row + 1
            ws.Range(Cells(2, 1), Cells(lrw, lc)).Copy
            sh.Range("A" & lr).PasteSpecial xlPasteValues
        End If
    Next ws
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    MsgBox "completed"
End Sub
 
Last edited:
Upvote 0
Thanks for your help Alan

On the line 'lrw = ws.Range("A" & Rows.Count).End(x1Up).Row' I'm getting a variable not defined error on (x1Up)

Also, I should have mentioned I need it to ignore sheet12(Lists) which contains the lists for drop-downs
 
Upvote 0
Change this line
Code:
If ws.Name <> "Master" Then
to
Code:
If ws.Name <> "Master" And ws.Name <> "Lists" Then
and to correct the error it should be xlup not x1up (ie lower case L, not number one)
 
Upvote 0
Please ignore last error, sorted it but I get an error "Method 'Range' of object '_Worksheet' failed" on the line "sh.Range("A" & lr).PasteSpecial xlPasteValues"
 
Upvote 0
I'm surprised you get it it on that line, rather than the previous line, which should be
Code:
            ws.Range(ws.Cells(2, 1), ws.Cells(lrw, lc)).Copy
 
Upvote 0
Awesome, nearly there

It's leaving the data on the original sheets though, ideally I need it to cut the rows or delete them after copy
 
Upvote 0
Add this just before the "End If" line
Code:
ws.Range(ws.Cells(2, 1), ws.Cells(lrw, lc)).EntireRow.Delete
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,166
Members
448,870
Latest member
max_pedreira

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