VBA If Then Statement

jtbrolly

New Member
Joined
Aug 22, 2011
Messages
42
Hey guys,

Not that good with coding, trying to make an if-then statement basically saying if it says something in column c, then write this with this formatting in column a. I've been looking all over for the exact coding and can't seem to find how to do it. Would really appreciate any help.

Thanks,
Jimmy
 
Using the same code, but updated with what I think you need extra..

Sub Test()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim LR As Long, c As Range<o:p></o:p>
LR = Range("c" & Rows.Count).End(xlUp).Row<o:p></o:p>
For Each c In Range("c1:c" & LR)<o:p></o:p>
If c.Value <> "" Then ‘ checking for blanks<o:p></o:p>
If c.Value = “099” then ‘ checking for your specified value<o:p></o:p>
c.Offset(0, -2).Value = "Default" ‘ I’ve made the assumption here you only wanted the word ‘default’?
<o:p> c.Offset(0,-2).interior.colorindex = 3</o:p>
Else: end if<o:p></o:p>
Else: End If<o:p></o:p>
Next c<o:p></o:p>
End Sub<o:p></o:p>

So the addition I've made just asks the procedure to check if the cell has the value your want highlighted and if it has, write the word default in column A.
 
Last edited:
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
Sub Test()
Dim LR As Long, c As Range
LR = Range("c" & Rows.Count).End(xlUp).Row
For Each c In Range("c1:c" & LR)
    If c.Value <> "" Then
        c.Offset(, -2).Value = "default"
    End If
Next c
    Columns("A:A").Select
    With Selection
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""default"""
    .FormatConditions(1).Interior.ColorIndex = 3
    End With
End Sub

This one is writing default and coloring for every cell, regardless of the contents of C.
 
Upvote 0
Using the same code, but updated with what I think you need extra..

Sub Test()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim LR As Long, c As Range<o:p></o:p>
LR = Range("c" & Rows.Count).End(xlUp).Row<o:p></o:p>
For Each c In Range("c1:c" & LR)<o:p></o:p>
If c.Value <> "" Then ‘ checking for blanks<o:p></o:p>
If c.Value = “099” then ‘ checking for your specified value<o:p></o:p>
c.Offset(0, -2).Value = "Default" ‘ I’ve made the assumption here you only wanted the word ‘default’?
<o:p>c.Offset(0,-2).interior.colorindex = 3</o:p>
Else: end if<o:p></o:p>
Else: End If<o:p></o:p>
Next c<o:p></o:p>
End Sub<o:p></o:p>

So the addition I've made just asks the procedure to check if the cell has the value your want highlighted and if it has, write the word default in column A.

This is it! Woohoo! Thank you so much!

Just two quick questions, how do I change the highlighted color to Yellow, I changed the color index to 6 but it's still coming up red.

And how do I add another number to look for. I'm trying this but it's not working:
If c.Value = “099” or 345 or "5JF" then
 
Upvote 0
This one is writing default and coloring for every cell, regardless of the contents of C.


sorry this is the code slightly changed the if then entry to o99

Code:
Sub Test()
Dim LR As Long, c As Range
LR = Range("c" & Rows.Count).End(xlUp).Row
For Each c In Range("c1:c" & LR)
    [B]If c.Value = "o99" Then
[/B]        c.Offset(, -2).Value = "default"
    End If
Next c
    Columns("A:A").Select
    With Selection
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""default"""
    .FormatConditions(1).Interior.ColorIndex = 3
    End With
End Sub
 
Upvote 0
now with all requirements

Code:
Sub Test()
Dim LR As Long, c As Range
LR = Range("c" & Rows.Count).End(xlUp).Row
For Each c In Range("c1:c" & LR)
    If c.Value = "o99" Or c.Value = "345" Or c.Value = "5JF" Then
        c.Offset(, -2).Value = "default"
    End If
Next c
    Columns("A:A").Select
    With Selection
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""default"""
    .FormatConditions(1).Interior.ColorIndex = 6
    End With
End Sub
 
Upvote 0
now with all requirements

Code:
Sub Test()
Dim LR As Long, c As Range
LR = Range("c" & Rows.Count).End(xlUp).Row
For Each c In Range("c1:c" & LR)
    If c.Value = "o99" Or c.Value = "345" Or c.Value = "5JF" Then
        c.Offset(, -2).Value = "default"
    End If
Next c
    Columns("A:A").Select
    With Selection
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""default"""
    .FormatConditions(1).Interior.ColorIndex = 6
    End With
End Sub


Works perfect! Great team work! Lol but in all seriousness thanks so much, you've both made my life 1000x easier.

Cheers! :)
 
Upvote 0
This is it! Woohoo! Thank you so much!

Just two quick questions, how do I change the highlighted color to Yellow, I changed the color index to 6 but it's still coming up red.

And how do I add another number to look for. I'm trying this but it's not working:
If c.Value = “099” or 345 or "5JF" then

How about this update? Select Case is a wonderful alternative to 'if-then' statements
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Sub Test()
Dim LR As Long, c As Range
LR = Range("c" & Rows.Count).End(xlUp).Row
For Each c In Range("c1:c" & LR)
Select case c.Value<o:p></o:p>
Case “099”,”345”,”5JF” ‘ you can add as many as you like, just copy this format<o:p></o:p>
c.Offset(0, -2).Value = "Default"<o:p></o:p>
c.Offset(0,-2).interior.colorindex = 6<o:p></o:p>
Case else ‘ by leaving it blank under ‘case else’ it’s the same as saying ‘do nothing’<o:p></o:p>
End select<o:p></o:p>
Next c
End Sub

Sorry about the colorindex number being wrong, I didnt check it this time but I have this page (http://dmcritchie.mvps.org/excel/colors.htm) saved for referencing colours normally
 
Upvote 0
How about this update? Select Case is a wonderful alternative to 'if-then' statements
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Sub Test()
Dim LR As Long, c As Range
LR = Range("c" & Rows.Count).End(xlUp).Row
For Each c In Range("c1:c" & LR)
Select case c.Value<o:p></o:p>
Case “099”,”345”,”5JF” ‘ you can add as many as you like, just copy this format<o:p></o:p>
c.Offset(0, -2).Value = "Default"<o:p></o:p>
c.Offset(0,-2).interior.colorindex = 6<o:p></o:p>
Case else ‘ by leaving it blank under ‘case else’ it’s the same as saying ‘do nothing’<o:p></o:p>
End select<o:p></o:p>
Next c
End Sub

Sorry about the colorindex number being wrong, I didnt check it this time but I have this page (http://dmcritchie.mvps.org/excel/colors.htm) saved for referencing colours normally

That's a good idea, I might try Case instead because I have to do a bunch more of these and these seems like complicated. So if I wanted to add another Case with a different value to put in A, where would I put that, after Case else or before?

Example: Case 21J put None in A and highlight yellow.
 
Upvote 0
That's a good idea, I might try Case instead because I have to do a bunch more of these and these seems like complicated. So if I wanted to add another Case with a different value to put in A, where would I put that, after Case else or before?

Example: Case 21J put None in A and highlight yellow.

Always before the case else, as thats your last stop-shop..

Case "your new value"
Code to execute here
Case else..
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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