If Row 1 = Row 2 then delete Row 2, else MsgBox

Binning

New Member
Joined
Apr 30, 2014
Messages
12
Hi there,

I'm trying to input a check into my macro. Basically the logic I'm trying to follow is:

If Row 1 = Row 2 then delete row 2,
ElseIf Row 1 <> Row 2 then Display MsgBox "Row 1 and Row 2 are different. Do you wish to continue?"
If user selects yes, resume
If user selects no, end sub and display MsgBox "Macro ended. Please correct changes manually"

Can anyone help me if this isn't too much trouble?

Thanks,
Binning
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Binning

New Member
Joined
Apr 30, 2014
Messages
12
Sorry I'm not sure about the rules on bumping here but this has now found it's way to page 6 with no reply.
 

Gavin T

Well-known Member
Joined
Mar 26, 2014
Messages
836

ADVERTISEMENT

You can try this code:

Code:
Sub Match_Rows()
    

    Dim rngCell     As Range
    Dim strResponse As String
    Dim lngCount     As Long

    For Each rngCell In Range("A1:AL1")
        If rngCell <> rngCell.Offset(1, 0) Then
            lngCount = lngCount + 1
        End If
    Next rngCell
    
    If lngCount = 0 Then
        Rows(2).Delete
    Else
        strResponse = MsgBox("Row 1 and Row 2 are different. Do you wish to continue?", vbYesNo)
        If strResponse = vbNo Then
            MsgBox ("Macro ended. Please correct changes manually")
            Exit Sub
        End If
    End If
End Sub


ElseIf Row 1 <> Row 2 then Display MsgBox "Row 1 and Row 2 are different. Do you wish to continue?"
If user selects yes, resume...

When you say resume do you want to have row 2 deleted?
 

Gavin T

Well-known Member
Joined
Mar 26, 2014
Messages
836
For Row 2 to be deleted when the user presses yes, use the following code:

Code:
Sub Match_Rows()
    

    Dim rngCell     As Range
    Dim strResponse As String
    Dim lngCount     As Long


    For Each rngCell In Range("A1:AL1")
        If rngCell <> rngCell.Offset(1, 0) Then
            lngCount = lngCount + 1
        End If
    Next rngCell
    

    If lngCount <> 0 Then
    strResponse = MsgBox("Row 1 and Row 2 are different. Do you wish to continue?", vbYesNo)
        If strResponse = vbNo Then
            MsgBox ("Macro ended. Please correct changes manually")
            Exit Sub
        End If
    End If
    Rows(2).Delete
End Sub

Good luck!
 

Watch MrExcel Video

Forum statistics

Threads
1,127,107
Messages
5,622,780
Members
415,927
Latest member
vedasinternational

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