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

strat919

Board Regular
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!
 

Michael M

Well-known Member
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
 

strat919

Board Regular
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?
 

Michael M

Well-known Member
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.
 

strat919

Board Regular
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
 

Michael M

Well-known Member
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:

strat919

Board Regular
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.
 

My Aswer Is This

Well-known Member
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.
 

Michael M

Well-known Member
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
 

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...
Top