create a button to do conditional formatting

kerm007

Active Member
Joined
Mar 16, 2019
Messages
266
Office Version
  1. 365
Platform
  1. Windows
hello
i try to do a conditional formating with a button
i have this code but its not right

VBA Code:
ub Highlight()
Dim MyRange As Range
'Create range object
Set MyRange = Sheets("Fixtures").Range("B:D")
'Delete previous conditional formats
MyRange.FormatConditions.Delete
'Add first rule
MyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=H", Formula2:="=150"
MyRange.FormatConditions(1).Interior.Color = RGB(255, 0, 0)
End Sub

i have number on colum B and F and some are Numbers with an H
i want to put in bold and red the line where its number and H
like 60H

can i do that ?
Thanks

ps: the button i create is on another sheet because when i ran the fixture the button disapear
ty
 

Attachments

  • Capture.PNG
    Capture.PNG
    15.4 KB · Views: 18
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
VBA Code:
Sub Highlight()
    Dim MyRange As Range
    Dim FormulaStr As String

    'Create range object
    Set MyRange = Sheets("Fixtures").Range("B:D")

    'Delete previous conditional formats
    MyRange.FormatConditions.Delete

    'Create Formula
    '=AND(RIGHT(B1,1)="H",ISNUMBER(IFERROR(VALUE(LEFT(B1,1)),"")))
    FormulaStr = "=AND(RIGHT(B1,1)=" & """" & "H" & """" & ",ISNUMBER(IFERROR(VALUE(LEFT(B1,1))," & """""" & ")))"
    Debug.Print FormulaStr

    'Add first rule
    MyRange.FormatConditions.Add xlExpression, Formula1:=FormulaStr
    MyRange.FormatConditions(1).Interior.Color = RGB(255, 0, 0)
End Sub
 
Upvote 0
Thanks it work
the only thing i miss is how it can put in the whole line
i change the Set MyRange = Sheets("Fixtures").Range("B:D") for Set MyRange = Sheets("Fixtures").Range("B:F")
also how can the text in the cell be in bold ?
for the rest its perfect TY
 
Upvote 0
I don't know what you mean by "put it in the whole line"
 
Upvote 0
i mean instead of the cell i will give you an example :-)
thanks
 

Attachments

  • line.PNG
    line.PNG
    12.9 KB · Views: 10
Upvote 0
VBA Code:
Sub Highlight()
    Dim MyRange As Range
    Dim FormulaStr As String
    
    'Create range object
    Set MyRange = ActiveSheet.Range("B:D")            'Sheets("Fixtures").Range("B:D")
    
    'Delete previous conditional formats
    MyRange.FormatConditions.Delete
    
    'Create Formula
    '=AND(RIGHT(B1,1)="H",ISNUMBER(IFERROR(VALUE(LEFT(B1,1)),"")))
    FormulaStr = "=AND(RIGHT($B1,1)=" & """" & "H" & """" & ",ISNUMBER(IFERROR(VALUE(LEFT($B1,1))," & """""" & ")))"
    
    'Add first rule
    With MyRange.FormatConditions.Add(xlExpression, Formula1:=FormulaStr)
        .Interior.Color = RGB(255, 0, 0)
        .Font.Bold = True
        .Font.Color = vbWhite
    End With
    
    FormulaStr = "=AND(RIGHT($C1,1)=" & """" & "H" & """" & ",ISNUMBER(IFERROR(VALUE(LEFT($C1,1))," & """""" & ")))"
    
    'Add 2nd rule
    With MyRange.FormatConditions.Add(xlExpression, Formula1:=FormulaStr)
        .Interior.Color = RGB(255, 0, 0)
        .Font.Bold = True
        .Font.Color = vbWhite
    End With
    
    FormulaStr = "=AND(RIGHT($D1,1)=" & """" & "H" & """" & ",ISNUMBER(IFERROR(VALUE(LEFT($D1,1))," & """""" & ")))"
    
    'Add 3rd rule
    With MyRange.FormatConditions.Add(xlExpression, Formula1:=FormulaStr)
        .Interior.Color = RGB(255, 0, 0)
        .Font.Bold = True
        .Font.Color = vbWhite
    End With
End Sub

If you want to extend the range to B:F (or anything else), I think it is obvious where to change the range in the example. You will also need to add 2 more rules.
 
Upvote 0
Solution
hello i try the code and nothing happen but the first code you gave me did the job but for the cell only i think i will leave it like this
Thanks
 
Upvote 0
hello ok it work but only when the number and the H is there on the Left colum B
if the number and the H is on the right side colum F it does nothing
should i add this ?

FormulaStr = "=AND(RIGHT($F1,1)=" & """" & "H" & """" & ",ISNUMBER(IFERROR(VALUE(LEFT($F1,1))," & """""" & ")))"

and if yes where ?
TY
 
Upvote 0
hello never mind i figure it out i forgot part of the code at first then i see the second formulat that need to be change also it work well TY
 
Upvote 0
one less question if i want to add another look for another word call No Game
should i add a FormulaStr and a rules ?
or the rules is enough ?
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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