Highlight cells without a formula

scotthannaford1973

Board Regular
Joined
Sep 27, 2017
Messages
114
Office Version
  1. 2010
Platform
  1. Windows
Hi all

I have a sheet with 100 or so rows in - and every row has the same formulas; as I have staff who aren't great on Excel, they sometimes delete formulas or insert rows (and then don't add in the formulas). I'd like to add in some VB that will, for example, look at range A1:100 and highlight any cells that don't have a formula in. They may have data or conditional formatting, but by clicking on a button to run the module I want to show the cells where there are no formulas. Is that possible?

TIA
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi,

Do you mean you would need a UDF
VBA Code:
Function isFormula(ByVal target As Range) As Boolean
     isFormula = target.hasFormula
End Function
or run a macro with the same approach to change the format of each cell which .HasFormula ...
 
Upvote 0
For the test macro ...
Code:
Sub Test()
Dim c As Range
For Each c In Selection
    If c.HasFormula = True Then
      c.Interior.Color = vbYellow
    End If
Next c
End Sub
 
Upvote 0
Hi James,

Sorry for intrupting

Macro works fine but running very slow
also can we add a condition so that when formula is added and we run macro again then it selects No fill for those new added formulas.
 
Upvote 0
Hi,
Revised macro for speed ...;)
VBA Code:
Sub Test()
Dim c As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each c In Selection
    If c.HasFormula = True And Not (c.Interior.Color = vbYellow) Then
      c.Interior.Color = vbYellow
    End If
Next c
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Maybe you could play with something like below, it will apply the format all at once when it has built a range from finding blanks or constants within the given range - A1:A100 in this case:

VBA Code:
Sub test()
    Dim rng As Range, bRng As Range, fRng As Range, uRng As Range
    
    Set rng = Range("A1:A100")
    
    On Error Resume Next
    Set bRng = rng.SpecialCells(xlCellTypeBlanks) ' blanks
    Set fRng = rng.SpecialCells(xlCellTypeConstants) ' constants
    On Error GoTo 0
    
    If bRng Is Nothing And fRng Is Nothing Then
        Exit Sub
    ElseIf Not bRng Is Nothing And Not fRng Is Nothing Then
        Set uRng = Union(bRng, fRng)
    ElseIf Not bRng Is Nothing Then
        Set uRng = bRng
    ElseIf Not fRng Is Nothing Then
        Set uRng = fRng
    End If
    
    uRng.Interior.Color = vbYellow
End Sub
 
Upvote 0
Solution
Hi,
Revised macro for speed ...;)
VBA Code:
Sub Test()
Dim c As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each c In Selection
    If c.HasFormula = True And Not (c.Interior.Color = vbYellow) Then
      c.Interior.Color = vbYellow
    End If
Next c
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

Thank you James,
Working perfect.
I found the reason why it was running slow, becuase I was selecting entire column.
 
Upvote 0
Maybe you could play with something like below, it will apply the format all at once when it has built a range from finding blanks or constants within the given range - A1:A100 in this case:

VBA Code:
Sub test()
    Dim rng As Range, bRng As Range, fRng As Range, uRng As Range
   
    Set rng = Range("A1:A100")
   
    On Error Resume Next
    Set bRng = rng.SpecialCells(xlCellTypeBlanks) ' blanks
    Set fRng = rng.SpecialCells(xlCellTypeConstants) ' constants
    On Error GoTo 0
   
    If bRng Is Nothing And fRng Is Nothing Then
        Exit Sub
    ElseIf Not bRng Is Nothing And Not fRng Is Nothing Then
        Set uRng = Union(bRng, fRng)
    ElseIf Not bRng Is Nothing Then
        Set uRng = bRng
    ElseIf Not fRng Is Nothing Then
        Set uRng = fRng
    End If
   
    uRng.Interior.Color = vbYellow
End Sub

Thanks for the reply Georgyboy, I have changed range to B1:B100

Not working, by the way James's code works so no need to modify the code, just wanted to let you know.

Book2
ABCD
10
20
30
40
50
60
70
80
90
100
110
12
13
140
150
16
17
180
19
Sheet1
Cell Formulas
RangeFormula
B1:B11,B18,B14:B15B1=A1-C1
 
Upvote 0
Glad to hear you could fix your problem :)

The final code looks like

VBA Code:
Sub test()
Dim c As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each c In Selection
    If c.HasFormula = True Then
      c.Interior.Color = vbYellow
      Else
      c.Interior.Color = xlNone
    End If
Next c
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,616
Messages
6,125,865
Members
449,266
Latest member
davinroach

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