Pasting issue with VBA (Simple)

bran8989

New Member
Joined
Sep 14, 2018
Messages
23
Looking for help on this, I'm erroring out on my pasting line...

Sub TEST()


i = 1
n = 2
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row


For n = 2 To lastrow


With ActiveSheet
.Range("A" & n, Range("A" & n).End(xlToRight)).Cut
.Range("A" & n).End(xlToRight).Paste
End With


Next n


End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Code:
With ActiveSheet
.Range("A" & n, Range("A" & n).End(xlToRight)).Cut .Range("A" & n).End(xlToRight)
End With

Try this, do you want to cut and paste to the same location?
Actually, this will probably error, because there is insufficient room to paste if there is no other data to the rigtht.
 
Last edited:
Upvote 0
If your range is A2:D2, you're cutting and placing it into D2:G2?
Try:
Code:
Sub Test()

    Dim LR  As Long
    Dim x   As Long
    Dim y   As Long
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        LR = .Cells(.Rows.Count, 1).End(xlUp).Row
        For x = 2 To LR
            y = .Cells(x, .Columns.Count).End(xlToLeft).Column
            .Cells(x, y).Resize(, y).Value = .Cells(x, 1).Resize(, y).Value
            .Cells(x, 1).Resize(, y - 1).ClearContents
        Next x
    End With
    
    Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
If your range is A2:D2, you're cutting and placing it into D2:G2?
Try:
Code:
Sub Test()

    Dim LR  As Long
    Dim x   As Long
    Dim y   As Long
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        LR = .Cells(.Rows.Count, 1).End(xlUp).Row
        For x = 2 To LR
            y = .Cells(x, .Columns.Count).End(xlToLeft).Column
            .Cells(x, y).Resize(, y).Value = .Cells(x, 1).Resize(, y).Value
            .Cells(x, 1).Resize(, y - 1).ClearContents
        Next x
    End With
    
    Application.ScreenUpdating = True

End Sub

Both of these work thanks!!!
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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