vba code to define conditions for conditional formatting

mdd16

Board Regular
Joined
Jan 11, 2011
Messages
84
Office Version
  1. 365
Platform
  1. Windows
Hello

I need help with a code that applies conditional formatting for 'all cells in selected range', having contents which meet following criteria
  • Is Not Blank
  • Is Not a Formula
  • Is Not Text
Basically all non blank cells with content that is numbers

Can someone help please.
Thanks in advance.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You haven't said what format you want, but here's a demo on how to achieve some formatting on a selection of cells. Try it on a copy of your sheet.
VBA Code:
Option Explicit
Sub Format_Selection()
    Dim r As String
    With Selection
        r = Replace(.Cells(1).Address, "$", "")
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(ISNUMBER(" & r & "),ISFORMULA(" & r & ")=FALSE)"
        .FormatConditions(1).Interior.Color = vbYellow
        .FormatConditions(1).StopIfTrue = False
    End With
End Sub

Before:
Book1
GHIJ
45
5Hello
62023
7123123AA
8
91
1027/08/2023
111
Sheet1
Cell Formulas
RangeFormula
J4J4=H9*5
H9H9=1*1
I10I10=TODAY()


After:
Book1
GHIJ
45
5Hello
62023
7123123AA
8
91
1027/08/2023
111
Sheet1
Cell Formulas
RangeFormula
J4J4=H9*5
H9H9=1*1
I10I10=TODAY()
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G4:J11Expression=AND(ISNUMBER(G4),ISFORMULA(G4)=FALSE)textNO
 
Upvote 0
Hello Kevin and thanks for your quick help.. your code works great.

However I have additional needs. I have created a user form with 2 option buttons. One to apply the conditional formatting and another remove those interior and font colors. But the removal code which used to work fine does not work now. I have attempted to remove the conditional formatting but it did not work. My code is given below;

VBA Code:
Sub EntryCellOptions_Show()

Fm_EntryFormat_YN.OpBn_EntFmtApply_Sln.value = False
Fm_EntryFormat_YN.OpBn_EntFmtRmv_Sln.value = False
Fm_EntryFormat_YN.Show

End Sub
Sub eFmt_Selection()
    Dim r As String
    With Selection
        r = Replace(.Cells(1).Address, "$", "")
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(ISNUMBER(" & r & "),ISFORMULA(" & r & ")=FALSE)"
        .FormatConditions(1).Interior.Color = RGB(255, 255, 224)
        .FormatConditions(1).Font.Color = RGB(220, 20, 60)
        .FormatConditions(1).StopIfTrue = False

    End With
            
End Sub

Sub EntryCell_FmtErase_Sln()
Dim rCell As Range

    For Each rCell In Selection
        If rCell.Interior.Color = RGB(255, 255, 224) Then  'light yellow
            rCell.FormatConditions.Delete
            rCell.Interior.Color = RGB(255, 255, 255)      'no color
        End If
    Next rCell

      For Each rCell In Selection
        If rCell.Font.Color = RGB(220, 20, 60) Then  'red or crimson
            rCell.FormatConditions.Delete
            rCell.Font.Color = RGB(64, 64, 64) 'light black
        End If
    Next rCell

End Sub
 
Upvote 0
Have you considered simply:
VBA Code:
Sub EntryCell_FmtErase_Sln()
    With Selection
        .FormatConditions.Delete
    End With
End Sub
 
Upvote 0
Sub EntryCell_FmtErase_Sln() With Selection .FormatConditions.Delete End With End Sub
Thanks again Kevin.. this works.

However this will delete conditional formatting in all the cells whereas I wanted to reformat only those cells with my 'entry cell format'.

If you can further suggest something, will be nice.. but meanwhile I will implement what you suggest and keep looking for better solution..

Thanks again for your help mate..
 
Upvote 0
In that case, try the following (simply reversing the use of the tests)
VBA Code:
Sub EntryCell_FmtErase_Sln()
    Application.ScreenUpdating = False
    Dim rCell As Range
    For Each rCell In Selection
        If IsNumeric(rCell) And WorksheetFunction.IsFormula(rCell) = False Then
            rCell.FormatConditions.Delete
        End If
    Next rCell
    Application.ScreenUpdating = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,359
Messages
6,124,488
Members
449,165
Latest member
ChipDude83

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