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

#### Binning

##### New Member
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

### Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

#### Binning

##### New Member
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
If Row 1 = Row 2 then...

What do you mean by Row 1 = Row 2?

Do you mean that each cell in Row 1 is equal to the cell in Row 2 that's directly beneath it?

#### Binning

##### New Member
Yes Gavin, i.e. A1:AL1 = A2:AL2

#### Gavin T

##### Well-known Member

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?

#### Binning

##### New Member
Thanks Gav, this is perfect.

Yeh I wanted row 2 deleted

#### Gavin T

##### Well-known Member
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!

Replies
3
Views
65
Replies
9
Views
158
Replies
2
Views
170
Replies
3
Views
106
Replies
5
Views
58

1,127,212
Messages
5,623,420
Members
415,974
Latest member
ZorroOP

### 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.

### Which adblocker are you using?

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

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