run-time error 1004 : Application-defined or object-defined error

manvit

New Member
Joined
Oct 31, 2018
Messages
19
Hi Excel folks,

I m trying to move every second row at the end of previous row.
This below code is being applied from the third row. I m getting the desired out , but i m also getting the below error.
" run-time error 1004 : Application-defined or object-defined error".

I m aware that error is in the below red line, but why is that an error
Can anyone help me out with where is the error in this code?

sub merging()
Dim sh As Worksheet, lr As Long, lc As Long
Set sh = Sheet4 'Edit sheet name.
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 1 Step -2
With sh
.Range(.Cells(i, 1), .Cells(i, Columns.Count).End(xlToLeft)).Copy .Cells(i - 1, Columns.Count).End(xlToLeft).Offset(0, 1)
.Rows(i).Delete
End With
Next
End Sub

any help will be very appreciated

Best,
Manvit
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Maybe this

Code:
Sub merging()
Dim sh As Worksheet, lr As Long, lc As Long
Set sh = Sheet4 'Edit sheet name.
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 1 Step -2
    lc = ActiveSheet.Cells(i, Columns.Count).End(xlToLeft).Column
        With sh
            .Range(.Cells(i, 1), .Cells(i, lc)).Copy Cells(i - 1, lc + 1)
            .Rows(i).Delete
        End With
Next i
End Sub
 
Last edited:
Upvote 0
Maybe this

Code:
Sub merging()
Dim sh As Worksheet, lr As Long, lc As Long
Set sh = Sheet4 'Edit sheet name.
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 1 Step -2
    lc = ActiveSheet.Cells(i, Columns.Count).End(xlToLeft).Column
        With sh
            .Range(.Cells(i, 1), .Cells(i, lc)).Copy Cells(i - 1, lc + 1)
            .Rows(i).Delete
        End With
Next i
End Sub
Thanks for trying Michael but this too is giving me a same error! :(
 
Upvote 0
Do you have an odd number of rows ??
It will error if ste-2 can't find a row to work with !!
 
Upvote 0
Change

For i = lr To 1 Step -2

to

For i = lr To 2 Step -2
 
Upvote 0
As you were trying to move data to the previous row, when it got to row 1, there was nowhere for the data to be moved to....so an error occured.
When the loop only goes to row 2, it finishes correctly.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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