IF this but NOT this OR this

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm using an IF statement to merge cells matching cells in a column - gotta make stuff pretty after all /s

Anyway, my code looks like this

VBA Code:
        Application.DisplayAlerts = False
Recheck:
        For Each cell In Range("C4:C148", Range("C4").End(xlToRight))
            If cell.Value = cell.Offset(1, 0) And cell.Value <> "" Then
                Range(cell, cell.Offset(1, 0)).Merge
                GoTo Recheck
            End If
        Next
        Application.DisplayAlerts = False

Which works fine.

However, I would like to add a second condition after the And - so there would basically be two things to ignore; the first being blanks and the second being "stringystring"

VBA Code:
        Application.DisplayAlerts = False
Recheck:
        For Each cell In Range("C4:C148", Range("C4").End(xlToRight))
            If cell.Value = cell.Offset(1, 0) And (cell.Value <> "" Or cell.Value <> "stringystring") Then
                Range(cell, cell.Offset(1, 0)).Merge
                GoTo Recheck
            End If
        Next
        Application.DisplayAlerts = False

trying this code just causes the code to loop indefinitely and excel stops responding. I think this is something to do with me using "<>" twice. I've used it in the first example to say "cell value doesn't equal blank" but I think I'm misusing it where I should be using a Not operator?

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Answered my own question almost as soon as I'd asked it - the following works fine

Can anyone confirm my misuse of two "<>" in the previous code?

VBA Code:
Recheck:
        For Each cell In Range("C4:C148", Range("C4").End(xlToRight))
            If cell.Value = cell.Offset(1, 0) And (Not (cell.Value = "" Or cell.Value = "stringystring")) Then
                Range(cell, cell.Offset(1, 0)).Merge
                GoTo Recheck
            End If
        Next
        Application.DisplayAlerts = False
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,215,026
Messages
6,122,738
Members
449,094
Latest member
dsharae57

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