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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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,224,820
Messages
6,181,157
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