highlight cells which are not based on a formula

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
I have a range (col D) which is based on a formula. However, the user can "override" this formula by entering the correct values, instead of calculating.

See enclosed (simplified) example.

How can I (VBA) highlight the cells which are being entered manually ?

Thanks
 

Attachments

  • sum.png
    sum.png
    9.9 KB · Views: 4

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
This is a test for if a formula is in the cell. But, why do you want the user to delete the formula? The formula will be removed.

Excel Formula:
=ISFORMULA(D4)
 
Upvote 0
Thanks Awoohaw

so in VBA this would be:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim kol_D_rng As Range

Set kol_D_rng = Range("D2:D4")

If Not Application.Intersect(Target, kol_D_rng) Is Nothing Then
    If Target.HasFormula = False Then
        Target.Interior.Color = RGB(200, 200, 200)  'or some other RGB
    Else
    End If
End If

end sub

I needed this because I have some 1000 lines in column D, and the user must have the possibility of manually entering the value, instead of calculating it.

As I said, the initial example was very simplified, in reality the formula is much more complicated, and the entering of non-calculated results must remain possible.

Thanks again.
 
Upvote 0
I would suggest a few changes. Some of these things may be unlikely but, using my sample sheet below,
  • If the user enters a constant and then decides to put/copy the formula back in, your code will leave the cell coloured.
  • If the user decides to paste 11 in D3 but accidentally selects D3 & E3 before pasting, both D3 and E3 will get coloured (& removing the 11 from E3 would not remove the colour from that cell)
robertvdb.xlsm
ABCDE
1
25510
33811
410111
5
Sheet1
Cell Formulas
RangeFormula
D2:D4D2=A2+B2


This would be my suggestion for an alternative.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim kol_D_rng As Range, kol_D_formulas As Range
  
  Set kol_D_rng = Range("D2:D4")
  If Not Intersect(Target, kol_D_rng) Is Nothing Then
    Application.ScreenUpdating = False
    kol_D_rng.Interior.Color = RGB(200, 200, 200)
    On Error Resume Next
    Set kol_D_formulas = kol_D_rng.SpecialCells(xlFormulas)
    On Error GoTo 0
    If Not kol_D_formulas Is Nothing Then kol_D_formulas.Interior.ColorIndex = xlNone
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Thanks Peter

I'll have a look. Might be a useful amendment of the code.
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,016
Members
448,936
Latest member
almerpogi

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