How highlight range based on formula contents when select cell

leap out

Active Member
Joined
Dec 4, 2020
Messages
288
Office Version
  1. 2016
  2. 2010
Hi ,
in column g contains formulas . what I want when select cell in column G should highlight range based on formula contents.
for instance when select cell G2 . th formula contains =F2+F5+F8+F9+F10
RF.xlsm
ABCDEFG
1DATETYPEBRANDQTYUNIT PRICETOTAL
201/01/2020OILQ8 20W50 4x4L200.0020.004,000.0082,185.00
302/01/2020OILCAS 20W50 4x4L220.0024.005,280.00
403/01/2020OILCAS 10W40 4x4L120.0029.003,480.00
504/01/2020OILQ8 10W40 4x4L140.0022.003,080.00
604/01/2020OILEN 10W40 4x4L150.0044.006,600.0016,600.00
705/01/2020OILEN 15W40 4x4L200.0050.0010,000.00
805/01/2020OILQ8 15W40 4x4L111.0055.006,105.00
906/01/2020OILQ8 15W40 208L200.00250.0050,000.00
1007/01/2020OILQ8 5W30 208L100.00190.0019,000.00
1107/01/2020OILCAS 10W40 20L200.0060.0012,000.0020,760.00
1208/01/2020BATTERYXX 70A L U200.00120.0024,000.0046,000.00
1309/01/2020BATTERYXX 60A R V200.00110.0022,000.00
1410/01/2020BATTERYPOWER 100A L FG500.00400.00200,000.00800,000.00
1511/01/2020BATTERYPOWER 100A L FG600.00450.00270,000.00
1612/01/2020BATTERYPOWER 90A L FG600.00550.00330,000.00
1713/01/2020BATTERYDNG 90A L FG600.00530.00318,000.00318,000.00
OOL
Cell Formulas
RangeFormula
G2G2=F2+F5+F8+F9+F10
G6,G12G6=F6+F7
G11G11=F11+F4+F3
G14G14=F14+F15+F16
F2:F17F2=D2*E2
G17G17=F17



RF.xlsm
G
282,185.00
OOL
Cell Formulas
RangeFormula
G2G2=F2+F5+F8+F9+F10




then should highlight range for each cell is existed in the formula like this
RF.xlsm
ABCDEFG
1DATETYPEBRANDQTYUNIT PRICETOTAL
201/01/2020OILQ8 20W50 4x4L200.0020.004,000.0082,185.00
302/01/2020OILCAS 20W50 4x4L220.0024.005,280.00
403/01/2020OILCAS 10W40 4x4L120.0029.003,480.00
504/01/2020OILQ8 10W40 4x4L140.0022.003,080.00
604/01/2020OILEN 10W40 4x4L150.0044.006,600.0016,600.00
705/01/2020OILEN 15W40 4x4L200.0050.0010,000.00
805/01/2020OILQ8 15W40 4x4L111.0055.006,105.00
906/01/2020OILQ8 15W40 208L200.00250.0050,000.00
1007/01/2020OILQ8 5W30 208L100.00190.0019,000.00
1107/01/2020OILCAS 10W40 20L200.0060.0012,000.0020,760.00
1208/01/2020BATTERYXX 70A L U200.00120.0024,000.0046,000.00
1309/01/2020BATTERYXX 60A R V200.00110.0022,000.00
1410/01/2020BATTERYPOWER 100A L FG500.00400.00200,000.00800,000.00
1511/01/2020BATTERYPOWER 100A L FG600.00450.00270,000.00
1612/01/2020BATTERYPOWER 90A L FG600.00550.00330,000.00
1713/01/2020BATTERYDNG 90A L FG600.00530.00318,000.00318,000.00
OOL
Cell Formulas
RangeFormula
G2G2=F2+F5+F8+F9+F10
G6,G12G6=F6+F7
G11G11=F11+F4+F3
G14G14=F14+F15+F16
F2:F17F2=D2*E2
G17G17=F17

and if I select another cell in column G delete color for previous cell has ever selected.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Perhaps something like this.
VBA Code:
' Place code in worksheet module
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Application.Intersect(Me.Columns("G:G"), Me.UsedRange, Target) Is Nothing Then
        Dim rng As Range
        Dim FStr As String
        Dim S As Variant, SA As Variant

        With Me
            Set rng = .Range("A2:F" & .Range("A" & .Rows.Count).End(xlUp).Row)
            rng.Interior.ColorIndex = xlNone
        End With

        If Target.HasFormula Then
            FStr = Replace(Target.Formula, "=", "")
            FStr = Application.Trim(Replace(FStr, "+", " "))
            SA = Split(FStr)

            For Each S In SA
                Application.Intersect(rng, Me.Range(S).EntireRow).Interior.Color = vbYellow
            Next S
        End If
    End If
End Sub

Note that just as Col G formulas in your example only use the "+" operator, the above code works only for Col G formulas with just the "+" operator.
 
Upvote 0
I don't know why to knock down an open door. Select the cell and click the mouse in the formula entry box. The cell will be in edit state. The cells that are the source of the formula will be highlighted.

Artik
 
Upvote 0
Select the cell and click the mouse in the formula entry box. The cell will be in edit state. The cells that are the source of the formula will be highlighted.
I know and this doesn't solve my problem !
I would do that until check if I select right cells for th ID to calculation , then when highlight the ranges for the formula I can distinguish them and make easy to make sure to know if the ID is correct to calculating .
 
Upvote 0
wow !
this is really awesome !
can I implement your code by loop for all of sheets in workbook ,please?

Not by loop, because it's event code. But you could paste the same Private Sub Worksheet_SelectionChange(ByVal Target As Range) macro into the code module of each worksheet you want that behavior for.

Or you could convert the macro to workbook-level event code (not tested)
VBA Code:
' Place code in workBOOK code module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Not Application.Intersect(Sh.Columns("G:G"), Sh.UsedRange, Target) Is Nothing Then
        Dim rng As Range
        Dim FStr As String
        Dim S As Variant, SA As Variant

        With Sh
            Set rng = .Range("A2:F" & .Range("A" & .Rows.Count).End(xlUp).Row)
            rng.Interior.ColorIndex = xlNone
        End With

        If Target.HasFormula Then
            FStr = Replace(Target.Formula, "=", "")
            FStr = Application.Trim(Replace(FStr, "+", " "))
            SA = Split(FStr)

            For Each S In SA
                Application.Intersect(rng, Sh.Range(S).EntireRow).Interior.Color = vbYellow
            Next S
        End If
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,098
Messages
6,170,103
Members
452,302
Latest member
TaMere

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