VBA Copy/Paste then Delete Row

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
150
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
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
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
 
Upvote 0
@maras
If the OP has data in the blank columns (which is more than likely) won't your code delete it all?
 
Upvote 0
Yes there is data in the blank columns and yes the code delete all of that data. Otherwise, the code worked perfectly.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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