VBA Macro to copy entire row to another sheet

leopard

New Member
Joined
Feb 20, 2010
Messages
8
Hi All,

I have written Macro to copy every row in "sheet1" 24 times into new sheet called "NewSheet". but :nervous: it keep giving me error message. Actually, I don't know why. Can any one please help.

here is my macro

Sub CopyRowsBook2()

Worksheets.Add().Name = "NewSheet"
Sheets("Sheet1").Select
' Find the last row of data
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

' Loop through each row
For x = 2 To FinalRow
Worksheets("sheet1").Cells(x, 1).Select
ActiveCell.EntireRow.Select
Selection.Copy
' Loop to copy every row 24 times
For i = 1 To 24

Sheets("NewSheet").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste

Next i

Sheets("Sheet1").Select


Next x
End Sub


Thanks in advance,
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
In second loop it gives me a message saying "Run time error '1004': Application-defined or object- defined error"
 
Upvote 0
Did you rename "Sheet1" to something else by chance? If so update your code to the name of the new sheet or try replacing

Sheets("Sheet1").Select

with

Sheet1.Select
 
Upvote 0
As bjurney asked, did you rename "Sheet1"? Check to make sure there are no leading/trailing spaces if you highlight the tab, like "Sheet1 " or " Sheet1".

Or - is Sheet1 hidden?
 
Upvote 0
sheet1 has its original name, i have done a little editing and this is my new macro, but it still not working, this is driving me crazy,
now i'm using original sheet1 and sheet 2 as a source and as a destination.

Sub CopyRowsBook2()


Sheets("Sheet1").Select
' Find the last row of data
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

' Loop through each row

Dim x As Integer
Dim i As Integer

For x = 2 To FinalRow
Worksheets("sheet1").Cells(x, 1).Select
ActiveCell.EntireRow.Select
Selection.Copy

For i = 1 To 24

Sheets("sheet2").Select
DestinationRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(DestinationRow, 1).Select
ActiveSheet.Paste

Next i

Sheets("Sheet1").Select


Next x
End Sub

if this work, plz let me know which office u r using 2003 or 2007 and if u plz attach the file that containing working macro.
thanks in advance.
 
Upvote 0
It is slow and there are easier ways, but it works fine. I am using excel2000, OS is WIN and Office is XP - on a laptop that is about toast; so lets presume we're safe in those issues.

Put the cursor any place in the sub. Start pressing F8 and step through the code. If you reduce VBIDE's window you can watch everything happen step-by-step. See where (how many inner or outer loops) it gets to when it errors.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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