VBA Conditional

Chassmin

New Member
Joined
Jul 11, 2017
Messages
6
Hello everybody

I am here because I am trying to write a very easy code.
The idea is that if in Column A it says TBR and at the same time in Column E it says No, the E cell where it says No turns into Yellow.

I wrote this code but it does not work. Please please help me :)

Thank you so much

Code:
Sub condition()
    If Range("A5, A1700") = "TBR" And Range("E5, E1700") = "No" Then
    
    Range("E:E").Selection.Interior.Color = 3
    
    End If
    
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Sub condition()

dim J as long
for j = 5 to 1700
if cells(j, 1) = "TBR" And cells(j, 5) = "No" Then

range("a" & j & ":e" & j).Selection.Interior.Color = 3

End If
next j
End Sub
 
Last edited:
Upvote 0
Sorry,

I was editing your original and did not realise [TAB] caused it to post.
The version I have posted colours 5 cells.
 
Upvote 0
VBA is case sensitive. In excel No=no but in VBA No<> no. You will need to either make sure your sheet has the correct case in column A and E or modify the code to make the value compared the same case as your strings like below.

Code:
If UCase(Cells(x, 1)) = "TBR" And UCase(Cells(x, "E")) = "NO" Then
 
Upvote 0
Try this:

This script looks down column A from row 1 to the last row in column "A" with data.
Code:
Sub Test()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To Lastrow
        If Cells(i, 1).Value = "TBR" And Cells(i, "E").Value = "No" Then Cells(i, "E").Interior.ColorIndex = 6
    Next
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hello, all the No have N and therefore there is only one string No and only one string for TBR all in cap letters as well.

What I am trying to do is that in the same row we have TBR and No, then the cell in column E of that row is coloured.

Technically I have to have No and TBR in the same row, I think that is what is missing?

Thank you
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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