VBA code to copy X amount of rows from all sheets and paste to new sheet

strat919

Board Regular
Joined
May 15, 2019
Messages
54
I'm searching for VBA code that would copy X amount of rows starting with 1A from all sheets to a new sheet. Say if I had 13 sheets, it would copy the first 20 rows of every sheet and paste to a new sheet with the 260 rows. I would like to be able to specify and change the 20 to any amount.

I've searched pretty hard and have not seen one that will do this. This may be simple for someone, but not me:)

Any help much appreciated!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Maybe this then...


Code:
Sub MM1()
Dim lr As Long, ws As Worksheet, ans As Long
lr = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row
ans = InputBox("How many rows do you want copied ? ")
For Each ws In Worksheets
    If ws.Name <> "Master" Then
        With ws
        .Rows("1:" & ans).Copy Sheets("Master").Range("A" & lr)
        lr = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1
        End With
    End If
Next ws
End Sub
 
Upvote 0
I failed to mention that your destination sheet in this case needs to be called "Master"
 
Upvote 0
I created a sheet called Master. I changed the
("How many rows do you want copied ? ") to ("20")

When I run the macro MM1 macro, the only option I get is create....not run. I must be doing something wrong?
 
Upvote 0
Where did you paste the code ?
Put it into the "This Workbook" module.
To run it press ALT + F8 and select MM1 from the list and press run
OR
you can assign it to a shape or button on the Master sheet if you want to.
 
Upvote 0
I created module under VBAProject(Distance Workbook.xlsm) which is my workbook name. I entered the code and changed as above.
It shows up in the Macro as MM1, but only create option is offered.

I have created quite a few modules, but have never seen only the create option.

By the way, how do you ad the code box in these threads?

Thanks
 
Upvote 0
If you click anywhere in the code itself then press F8, does it start at the first line...usually in yellow. continue with F8 to step through the code?
Also, try changing the name of the macro to something else, I have had rare instances of people having issues with MM1...as a cell reference instead of a macro.
 
Last edited:
Upvote 0
I just changed the name to CombineRowsToSheet and now get the run, but when I click on run, I get a dialog box to enter something in. The dialog box is named Microsoft Excel.... it has a 20 in it, and blank field....and an OK and Cancel buttons.
 
Upvote 0
I was one. I had trouble running scripts with the name MM1. Running Excel 2013. I just changed the script name.
If you click anywhere in the code itself then press F8, does it start at the first line...usually in yellow. continue with F8 to step through the code?
Also, try changing the name of the macro to something else, I have had rare instances of people having issues with MM1...as a cell reference instead of a macro.
 
Upvote 0
Have you modified the code I provided in any way ??
The line in red asks you to input a number defining how many rows on each sheet you want copied.....as per your request to be able to change the row to any amount !
If you simply enter 20 into the blank "box" and press OK, it will copy 20 rows ffrom each sheet
If you enter 5 into the blank "box" and press OK, it will copy 5 rows ffrom each sheet

Code:
Sub MM1()
Dim lr As Long, ws As Worksheet, ans As Long
lr = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row
[color=red]ans = InputBox("How many rows do you want copied ? ")[/color]
For Each ws In Worksheets
    If ws.Name <> "Master" Then
        With ws
        .Rows("1:" & ans).Copy Sheets("Master").Range("A" & lr)
        lr = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1
        End With
    End If
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,495
Messages
6,113,992
Members
448,538
Latest member
alex78

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