Macro Help - IF statement with OR and AND

MrMatt

New Member
Joined
Apr 26, 2012
Messages
30
Hi

I'm struggling to create a macro that will check each cell in Columns B and E and do the following:

if cell in column B = London or Washington or Paris or Brussels (there are dozens of cities but I won't list them all)!
and if cell in column E is blank then insert "text" into column E (effectively overwriting the blank 'value')
else leave the value in column E as it is

Any help greatly appreciated.

Thanks

Matt
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Rather than listing each City, would it also work if you said:

If each Cell in B is not blank and the cell in E is Blank insert a value into E?

And what would like like to be entered into E?
 
Upvote 0
Try the following:

Code:
Sub Teeroy()
sCheckcities = UCase("London Washington Paris Brussels") 'add as required
For Each rng In Intersect(Columns("B"), ActiveSheet.UsedRange)
    If InStr(sCheckcities, UCase(rng.Value)) <> 0 And rng.Offset(0, 3) = "" Then
        rng.Offset(0, 3) = "Text"
    End If
Next
End Sub
 
Upvote 0
Try the following:

Code:
Sub Teeroy()
sCheckcities = UCase("London Washington Paris Brussels") 'add as required
For Each rng In Intersect(Columns("B"), ActiveSheet.UsedRange)
    If InStr(sCheckcities, UCase(rng.Value)) <> 0 And rng.Offset(0, 3) = "" Then
        rng.Offset(0, 3) = "Text"
    End If
Next
End Sub

Thanks Teeroy, this looks like it should work. One question before I run the macro, is how would I handle a city like New York - should I put each city into it's own quote marks?
 
Upvote 0
The Instr just matches a substring so you don't need to collect the two parts as a city. You may get a situation where if you had both "New York" and "York", York would match as a city. If this were a problem you could change the string to include "|" at each end as a separator e.g

Code:
Sub Teeroy()
sCheckcities = UCase("|London|Washington|Paris|Brussels|New York|") 'add as required
For Each rng In Intersect(Columns("B"), ActiveSheet.UsedRange)
    If InStr(sCheckcities, "|" & UCase(rng.Value) & "|") <> 0 And rng.Offset(0, 3) = "" Then
        rng.Offset(0, 3) = "Text"
    End If
Next
End Sub
 
Upvote 0
The Instr just matches a substring so you don't need to collect the two parts as a city. You may get a situation where if you had both "New York" and "York", York would match as a city. If this were a problem you could change the string to include "|" at each end as a separator e.g

Code:
Sub Teeroy()
sCheckcities = UCase("|London|Washington|Paris|Brussels|New York|") 'add as required
For Each rng In Intersect(Columns("B"), ActiveSheet.UsedRange)
    If InStr(sCheckcities, "|" & UCase(rng.Value) & "|") <> 0 And rng.Offset(0, 3) = "" Then
        rng.Offset(0, 3) = "Text"
    End If
Next
End Sub

Thanks for the explanation. The macro works perfectly.

Appreciate your help on this.
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,798
Members
449,337
Latest member
BBV123

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