VBA If cells ...

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Good Day,

In working with Range.H11:AC180, if any of these cells have the words "ENTER" in them, I'd like to make that cell highlighted red.

Any suggestions of vba or cf??


Thanks,
pinaceous
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Conditional formatting.

Just select you entire range and then use the Conditional Formatting formula:
Excel Formula:
=H11="ENTER"
And then choose your desired formatting.
 
Last edited:
Upvote 0
Hey Joe4,

I do appreciate that and due to the nature of the sheet could you kindly show me the vba method?

I'm experimenting with the following:

VBA Code:
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "J").End(xlUp).Row
    For i = 1 To Lastrow
        If Cells(i, "J").Interior.Color = vbRed Then Cells(i, "J").Value = "ENTER"
    Next

Please let me know, if you can help!

R/
pinaceous
 
Upvote 0
Hey Joe4,

I do appreciate that and due to the nature of the sheet could you kindly show me the vba method?

I'm experimenting with the following:

VBA Code:
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "J").End(xlUp).Row
    For i = 1 To Lastrow
        If Cells(i, "J").Interior.Color = vbRed Then Cells(i, "J").Value = "ENTER"
    Next

Please let me know, if you can help!

R/
pinaceous
Isn't your logic flipped? Shouldn't you check if the cell contains "Enter" and then highlight red?
 
Upvote 0
Try turning the Macro Recorder on, do what @Joe4 told you , turn the Macro Recorder off and you will have the code to use.
 
Upvote 0
Like this?

VBA Code:
Option Explicit
Sub test()
Dim rng As Range, f, fCell As String
Set rng = ActiveSheet.UsedRange
Set f = rng.Find("ENTER", , xlValues, xlPart)
If Not f Is Nothing Then
    f.Interior.Color = vbRed
    fCell = f.Address
    Do
        Set f = rng.FindNext(f)
        If Not f Is Nothing Then f.Interior.Color = vbRed
    Loop Until f.Address = fCell
End If
End Sub
 
Upvote 0
In conditional formatting: =ISNUMBER(FIND("ENTER",H11))
In VBA:
VBA Code:
Sub HightlightENTER()
    Dim xCll As Range
    For Each xCll In Range("H11:AC180")
        If InStr(xCll.Value, "ENTER") > 0 Then
            xCll.Interior.Color = RGB(255, 0, 0)
        Else
            xCll.Interior.Pattern = xlNone
        End If
    Next xCll
End Sub
 
Upvote 0
eiloken,

Okay, thank you! This is very interesting now!

How would you turn on/off the
VBA Code:
 =ISNUMBER(FIND("ENTER KG!",H11))
conditional formating?

For example, if I wanted to run this CF on opening up my document then turn it off after it has been completed?

Or would this situation call for the sub approach?

Thanks,
-pinaceous
 
Upvote 0
eiloken,

Okay, thank you! This is very interesting now!

How would you turn on/off the
VBA Code:
 =ISNUMBER(FIND("ENTER KG!",H11))
conditional formating?

For example, if I wanted to run this CF on opening up my document then turn it off after it has been completed?

Or would this situation call for the sub approach?

Thanks,
-pinaceous
You can set a cell to value that you can use it for turn on or off your hightlight condition. For example:
in conditional formatting: =AND($A$1="ON",ISNUMBER(FIND("ENTER",H11))) this formula will highlight your cells when cell A1 = ON
in vba:
VBA Code:
Sub HightlightENTER()
    Dim xCll As Range
    Range("H11:AC180").Interior.Pattern = xlNone
    If Range("A1").Value = "ON" Then
        For Each xCll In Range("H11:AC180")
            If InStr(xCll.Value, "ENTER") > 0 Then
                xCll.Interior.Color = RGB(255, 0, 0)
            End If
        Next xCll
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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