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!
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,974
Office Version
2013
Platform
Windows
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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,974
Office Version
2013
Platform
Windows
I failed to mention that your destination sheet in this case needs to be called "Master"
 

strat919

Board Regular
Joined
May 15, 2019
Messages
54
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
Joined
Oct 27, 2005
Messages
18,974
Office Version
2013
Platform
Windows
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
Joined
May 15, 2019
Messages
54
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
Joined
Oct 27, 2005
Messages
18,974
Office Version
2013
Platform
Windows
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
Joined
May 15, 2019
Messages
54
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
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
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
Joined
Oct 27, 2005
Messages
18,974
Office Version
2013
Platform
Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,871
Messages
5,489,401
Members
407,686
Latest member
Chuck1960

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top