Simple If Else in vba program Had Method Range Global failed error

asar90

New Member
Joined
Sep 8, 2013
Messages
4
Sub Nationality()

Dim rng As Range
Dim cell As Variant

lRow = Range("R" & Rows.Count).End(xlUp).Row

Set rng = Range("R2:R500" & lRow)

For Each cell In rng

If Cells(i, "R").Value <> "UK" Then
Cells(i, "R").Interior.ColorIndex = 3
End If
Next

I'm New to excel vba,
It seems to be simple any help thanks in advance
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi and welcome to the forum,

Try the following setup instead:

Code:
Sub Nationality()


    Dim rng     As Range
    Dim cell    As Range
    Dim lRow    As Long


    lRow = Range("R" & Rows.Count).End(xlUp).Row
    Set rng = Range("R2:R" & lRow)
    
    For Each cell In rng
        If cell.Value <> "UK" Then
            cell.Interior.ColorIndex = 3
        End If
    Next


End Sub
Note, although this was a simple example, in future posts it will be easier if you include more detail describing what line the error was on, as well as an in-words description of what you were trying to achieve.
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,411
Members
449,449
Latest member
Quiet_Nectarine_

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