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
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,446
Office Version
  1. 2013
Platform
  1. Windows
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:

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,685
Office Version
  1. 365
Platform
  1. Windows
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:

bran8989

New Member
Joined
Sep 14, 2018
Messages
23
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!!!
 

Watch MrExcel Video

Forum statistics

Threads
1,109,337
Messages
5,528,108
Members
409,802
Latest member
joeino

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top