Swap and copy trick

HURTMYPONY

Board Regular
Joined
Oct 27, 2003
Messages
166
About 6 months ago, someone here gave me a solution to a macro I was building.

I needed a macro that:

1. Duplicated every row that had a value in H and added to bottom of sheet at first empty row.
2. On those duplicated rows, it swapped the values in Column A and B with the values in Column H and I.

It works beautifully:

Code:
Dim valA
Dim valB
Dim cel As Range
Dim rng As Range
Dim lastrow As Long

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Get first blank row
    lastrow = Range("A" & Rows.Count).End(xlUp).Row + 1
    
    'Build all of Col H
    Set rng = Range("H2:H" & lastrow - 1)
        
    'Check all of Col H for values
    For Each cel In rng
        'If has value
        If cel <> "" Then
            'Copy to blank row at end
            Range("A" & cel.Row & ":L" & cel.Row).Copy Range("A" & lastrow)
            'Save Col A value
            valA = Range("A" & lastrow)
            'Save Col B value
            valB = Range("B" & lastrow)
            'Copy HI to AB
            Range("H" & lastrow & ":I" & lastrow).Copy Range("A" & lastrow)
            'Put old A to H
            Range("H" & lastrow) = valA
            'Put old B to I
            Range("I" & lastrow) = valB
            lastrow = lastrow + 1
        End If
    Next cel
    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    
    
    Set cel = Nothing
    Set rng = Nothing
    
End Sub

I am now trying to modify this code for another sheet, but differences in the layout of the sheet, coupled with my ignorance with how this code actually works, is causing me problems.

I need it to do the same thing that it does on the original, but:

1. The header starts on Row 4, not on Row 1 like on the original.
2. Instead of swapping the values in H and I with A and B on the duplicated rows, I need it to simply swap G with F.
3. My new sheet goes out to Column AQ.

Can anyone help me modify this code to work? Or even better, explain WHY it works?

Thank you!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This blind modification I made correctly duplicates the rows, and swaps F and G on those duplicated rows, but it is now putting spaces in my data. Any advice?

Code:
Dim valA
Dim valB
Dim cel As Range
Dim rng As Range
Dim lastrow As Long

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Get first blank row
    lastrow = Range("A" & Rows.Count).End(xlUp).Row + 2
    
    'Build all of Col H
    Set rng = Range("G5:G" & lastrow - 1)
        
    'Check all of Col H for values
    For Each cel In rng
        'If has value
        If cel <> "" Then
            'Copy to blank row at end
            Range("A" & cel.Row & ":AQ" & cel.Row).Copy Range("A" & lastrow)
            'Save Col F value
            valA = Range("F" & lastrow)
            'Save Col G value
            valB = Range("G" & lastrow)
            'Copy G to F
            Range("G" & lastrow).Copy Range("F" & lastrow)
            'Put old F to G
            Range("G" & lastrow) = valA
            lastrow = lastrow + 1
        End If
    Next cel
    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    
    
    Set cel = Nothing
    Set rng = Nothing

Disregard the remarks in the code - I didn't correct them.
 
Upvote 0
This now works, but still triples the header when there is no data to consider. Since, on some of the sheets, there may be a legitimate reason for no data, this needs to be fixed by me.

Any advice on how to modify this to not execute if there is no data beyond Row 4?

Code:
Sub Macro6()
'
' Macro6 Macro
'
Dim valA
Dim valB
Dim cel As Range
Dim rng As Range
Dim lastrow As Long

    With Application
        .ScreenUpdating = True
        .EnableEvents = False
    End With

    'Get first blank row
    lastrow = Range("A" & Rows.Count).End(xlUp).Row + 1
    
    'Build all of Col g
    Set rng = Range("G5:G" & lastrow - 1)
        
    'Check all of Col g for values
    For Each cel In rng
        'If has value
        If cel <> "" Then
            'Copy to blank row at end
            Range("A" & cel.Row & ":AQ" & cel.Row).Copy Range("A" & lastrow)
            'Save Col F value
            valA = Range("F" & lastrow)
            'Save Col G value
            valB = Range("G" & lastrow)
            'Copy G to F
            Range("G" & lastrow).Copy Range("F" & lastrow)
            'Put old F to G
            Range("G" & lastrow) = valA
            lastrow = lastrow + 1
        End If
    Next cel
    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    
    
    Set cel = Nothing
    Set rng = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

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