Conditional formatting in VBA

bloomingflower

New Member
Joined
Mar 9, 2020
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I have one question: I am trying to figure out how to add to the middle of my recorded macro part that will take a kid name from user and mark it green in the whole spreadsheet. I have tried the code below, but it doesn't work (error object required).

Name = InputBox("Enter Kid's Name:")
Dim myrange As Range
Set myrange = Sheet1.Cells.Select
For Each cell In myrange.Cells
If cell.Value = Name Then
cell.Interior.Color = 4
End If
Next
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hopefully this will address the "object missing" issue
VBA Code:
Sub SetCondition()                                    '(Name As String)
    Dim Name As String
    Dim myrange As Range
    Dim FC As FormatCondition

    Set myrange = ActiveSheet.UsedRange

    Name = InputBox("Enter Kid's Name:")
    If Name <> "" Then
        If ActiveSheet.UsedRange.Cells.Count = 1 Then    'no data on active sheet, so add some for demonstration purposes
            Set myrange = ActiveSheet.Range("A1:Z50")
            myrange.Range("A1").Value = Name
        End If

        With myrange
            If .FormatConditions.Count > 0 Then
                .FormatConditions.Delete              ' clear existing format conditions
            End If
            .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=Name
            Set FC = .FormatConditions(1)
            With FC.Interior
                .Color = 12379352
                .TintAndShade = 0
            End With
            With FC.Font
                .Bold = True
                .Italic = False
                .TintAndShade = 0
            End With
        End With
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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