VBA Copy/Paste then Delete Row

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
128
Hello, I need help writing a VBA code that will copy/paste values into a row, then delete the row they were copied from. This is based on if the value in column D is the same. See before and after pictures.

Before:
1605295469797.png



After:
1605295718279.png
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

maras

New Member
Joined
Dec 12, 2019
Messages
34
Office Version
  1. 2010
Platform
  1. Windows
Row 1 of the data has headers.
VBA Code:
Sub Transposition_maras()
    Dim a(), af
    Dim i As Long, r As Long
    Dim c As Integer
    
    With Sheets("Sheet1").[A1].CurrentRegion
        a = .Value
        ReDim af(1 To UBound(a), 1 To 200)
        For i = 2 To UBound(a)
            If a(i, 4) = a(i - 1, 4) Then
                c = c + 1
                af(r, c) = a(i, 15)
                c = c + 1
                af(r, c) = a(i, 16)
            Else
                r = r + 1
                c = 15
                af(r, 4) = a(i, 4)
                af(r, c) = a(i, 15)
                c = c + 1
                af(r, c) = a(i, 16)
            End If
        Next
        With .Offset(1, 0)
            .ClearContents
            .Resize(r, UBound(af, 2)) = af
        End With
    End With
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,682
Office Version
  1. 365
Platform
  1. Windows
@maras
If the OP has data in the blank columns (which is more than likely) won't your code delete it all?
 

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
128
Yes there is data in the blank columns and yes the code delete all of that data. Otherwise, the code worked perfectly.
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

Based on the before and after images, this would produce the results in the "After" image.

VBA Code:
Sub t()
Dim i As Long
    With ActiveSheet
        For i = .Cells(Rows.Count, 4).End(xlUp).Row To 2 Step -1
            If .Cells(i, 4).Value = .Cells(i - 1, 4).Value Then
                .Range(.Cells(i, 15), .Cells(i, Columns.Count).End(xlToLeft)). _
                Copy .Cells(i - 1, Columns.Count).End(xlToLeft).Offset(, 1)
                .Rows(i).Delete
            End If
        Next
    End With
End Sub
 

maras

New Member
Joined
Dec 12, 2019
Messages
34
Office Version
  1. 2010
Platform
  1. Windows
Corrected code.
VBA Code:
Sub Transforming_maras()
    Dim a(), af, rws
    Dim i As Long, r As Long
    Dim c As Integer
    
    With Sheets("Sheet1").[A1].CurrentRegion
        a = .Value
        ReDim af(1 To UBound(a), 1 To 200)
        ReDim rws(1 To UBound(a))
        
        For i = 2 To UBound(a)
            If a(i, 4) = a(i - 1, 4) Then
                c = c + 1
                af(r, c) = a(i, 15)
                c = c + 1
                af(r, c) = a(i, 16)
            Else
                r = r + 1
                rws(r) = i
                c = 1
                af(r, c) = a(i, 15)
                c = c + 1
                af(r, c) = a(i, 16)
            End If
        Next
        ReDim Preserve rws(1 To r)
        rws = Application.Transpose(rws)
        With .Offset(1, 0)
            .ClearContents
            .Resize(r, 14) = Application.Index(a, rws, Application.Transpose(Evaluate("Row(1:" & 14 & ")")))
            .Offset(, 14).Resize(r, UBound(af, 2)).Value = af
        End With
    End With
End Sub
 

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
128
Corrected code.
VBA Code:
Sub Transforming_maras()
    Dim a(), af, rws
    Dim i As Long, r As Long
    Dim c As Integer
   
    With Sheets("Sheet1").[A1].CurrentRegion
        a = .Value
        ReDim af(1 To UBound(a), 1 To 200)
        ReDim rws(1 To UBound(a))
       
        For i = 2 To UBound(a)
            If a(i, 4) = a(i - 1, 4) Then
                c = c + 1
                af(r, c) = a(i, 15)
                c = c + 1
                af(r, c) = a(i, 16)
            Else
                r = r + 1
                rws(r) = i
                c = 1
                af(r, c) = a(i, 15)
                c = c + 1
                af(r, c) = a(i, 16)
            End If
        Next
        ReDim Preserve rws(1 To r)
        rws = Application.Transpose(rws)
        With .Offset(1, 0)
            .ClearContents
            .Resize(r, 14) = Application.Index(a, rws, Application.Transpose(Evaluate("Row(1:" & 14 & ")")))
            .Offset(, 14).Resize(r, UBound(af, 2)).Value = af
        End With
    End With
End Sub
Thank you! This worked perfect.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,898
Messages
5,627,526
Members
416,250
Latest member
darius_rebelo

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
Top