Dynamic update of cell value based on color

JLouis

Active Member
Joined
Jan 1, 2004
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Hello. I have this range as shown below. This is a variety of price change options.

I need the user to be able to select a cell in F3:J3, the cell turn a color and the highlighted cell value to be displayed in K3. Repeat this down to row 16, and then add the selected cells together. I've tried some functions but they don't work dynamically and won't change when a new cell is selected. I hope I've explained this correctly. Thank you for your time.

Cell Formulas
RangeFormula
F12:F16,F9,F7,F3:F5F3=E3*$F$2
G12:G16,G9,G7,G3:G5G3=E3*$G$2
H12:H16,H9,H7,H3:H5H3=E3*$H$2
I12:I16,I9,I7,I3:I5I3=E3*$I$2
J12:J16,J9,J7,J3:J5J3=E3*$J$2
K3K3=SumColor(K2,F3:J3)
F17:J17F17=SUM(F3:F9)
 

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.
For Selecting one Cell at each row try this Worksheet event code (Right Cilck on sheet name and then Select view code Then Paste code 1 or 2):
If you want Clear colors from Cells Select Cell K2.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Long, Lr As Long
Lr = Range("F" & Rows.Count).End(xlUp).Row
If Intersect(Target, Union(Range("F3:J" & Lr - 1), Range("K2"))) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("K2")) Is Nothing Then
Range("F3:J" & Lr - 1).Interior.Color = 16777215
Range("K2").Copy
Range("F3:J" & Lr - 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("K3:K" & Lr - 1).ClearContents
Range("K2").Activate
End If
If Not Intersect(Target, Range("F3:J" & Lr - 1)) Is Nothing Then
Target.Interior.Color = 65535
Range("K" & Target.Row).Value = Target.Value
End If
Application.EnableEvents = True
End Sub

For Select Multiple Cell from Each row:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Long, Lr As Long, j As Long, K As Double
Lr = Range("F" & Rows.Count).End(xlUp).Row
If Intersect(Target, Union(Range("F3:J" & Lr - 1), Range("K2"))) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("K2")) Is Nothing Then
Range("F3:J" & Lr - 1).Interior.Color = 16777215
Range("K2").Copy
Range("F3:J" & Lr - 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("K3:K" & Lr - 1).ClearContents
Range("K2").Activate
End If
If Not Intersect(Target, Range("F3:J" & Lr - 1)) Is Nothing Then
Target.Interior.Color = 65535
For j = 6 To 10
If Cells(Target.Row, j).Interior.Color = 65535 Then K = K + Cells(Target.Row, j).Value
Next j
Range("K" & Target.Row).Value = K
End If
Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
Thank you so much for the reply. As far as I can tell, both codes do the exact same thing as far as the behavior of the sheet. The range f3:j12 work correctly and the selected cell updates correctly and dynamically.

There are two issues with the code. One is you can select more than one cell within the row. This was true with both formulas. The 2nd issue is that you can select and color cells outside of the range f3:j12.

Any suggestions on tweaking the code to address those two issues?

Thanks again for the assistance.
 

Attachments

  • Untitled-1.jpg
    Untitled-1.jpg
    122.2 KB · Views: 3
Last edited:
Upvote 0
Thank you so much for the reply. As far as I can tell, both codes do the exact same thing as far as the behavior of the sheet. The range f3:j12 work correctly and the selected cell updates correctly and dynamically.

There are two issues with the code. One is you can select more than one cell within the row. This was true with both formulas. The 2nd issue is that you can select and color cells outside of the range f3:j12.

Any suggestions on tweaking the code to address those two issues?

Thanks again for the assistance.
EDIT: I have solved the 2nd issue by relocating some cells being used under the shown range, making the lr-1 work correctly.
 
Upvote 0
EDIT: I have solved the 2nd issue by relocating some cells being used under the shown range, making the lr-1 work correctly.
EDIT: So the WB now works correctly. I added my previous effort to your work and although not great technique, the end result works just fine. I marked the question solved. Here's the code, don't laugh...



Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Long, Lr As Long
Static Cl As Range
Static C2 As Range
Static C3 As Range
Static C4 As Range
Static C5 As Range
Static C6 As Range
Static C7 As Range
Static C8 As Range
Static C9 As Range
Static C10 As Range
If Not Intersect(Target, Range("f3:j3")) Is Nothing Then
If Not Cl Is Nothing Then Cl.Interior.ColorIndex = 0
Set Cl = Target
End If
'2
If Not Intersect(Target, Range("f4:j4")) Is Nothing Then
If Not C2 Is Nothing Then C2.Interior.ColorIndex = 0
Set C2 = Target
End If
'3
If Not Intersect(Target, Range("f5:j5")) Is Nothing Then
If Not C3 Is Nothing Then C3.Interior.ColorIndex = 0
Set C3 = Target
End If
'4
If Not Intersect(Target, Range("f6:j6")) Is Nothing Then
If Not C4 Is Nothing Then C4.Interior.ColorIndex = 0
Set C4 = Target
End If
'5
If Not Intersect(Target, Range("f7:j7")) Is Nothing Then
If Not C5 Is Nothing Then C5.Interior.ColorIndex = 0
Set C5 = Target
End If
'6
If Not Intersect(Target, Range("f8:j8")) Is Nothing Then
If Not C6 Is Nothing Then C6.Interior.ColorIndex = 0
Set C6 = Target
End If
'7
If Not Intersect(Target, Range("f9:j9")) Is Nothing Then
If Not C7 Is Nothing Then C7.Interior.ColorIndex = 0
Set C7 = Target
End If
'8
If Not Intersect(Target, Range("f10:j10")) Is Nothing Then
If Not C8 Is Nothing Then C8.Interior.ColorIndex = 0
Set C8 = Target
End If
'9
If Not Intersect(Target, Range("f11:j11")) Is Nothing Then
If Not C9 Is Nothing Then C9.Interior.ColorIndex = 0
Set C9 = Target
End If
'10
If Not Intersect(Target, Range("f12:j12")) Is Nothing Then
If Not C10 Is Nothing Then C10.Interior.ColorIndex = 0
Set C10 = Target
End If
Lr = Range("F" & Rows.Count).End(xlUp).Row
If Intersect(Target, Union(Range("F3:J" & Lr - 1), Range("K2"))) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("K2")) Is Nothing Then
Range("F3:J" & Lr - 1).Interior.Color = 16777215
Range("K2").Copy
Range("F3:J" & Lr - 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("K3:K" & Lr - 1).ClearContents
Range("K2").Activate
End If
If Not Intersect(Target, Range("F3:J" & Lr - 1)) Is Nothing Then
Target.Interior.Color = 65535
Range("K" & Target.Row).Value = Target.Value
End If
Application.EnableEvents = True
End Sub
 
Upvote 0
if you want add column C to Color & Sum it Values to K column, this is code:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Long, Lr As Long, j As Long, K As Double
Lr = Range("F" & Rows.Count).End(xlUp).Row
If Intersect(Target, Union(Range("C3:C" & Lr - 1), Range("F3:J" & Lr - 1), Range("K2"))) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("K2")) Is Nothing Then
Range("F3:J" & Lr - 1).Interior.Color = 16777215
Range("K2").Copy
Range("F3:J" & Lr - 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("K3:K" & Lr - 1).ClearContents
Range("K2").Activate
End If
If Not Intersect(Target, Union(Range("C3:C" & Lr - 1), Range("F3:J" & Lr - 1))) Is Nothing Then
Target.Interior.Color = 65535
For j = 3 To 10
If Cells(Target.Row, j).Interior.Color = 65535 Then K = K + Cells(Target.Row, j).Value
if j = 3 Then j = j + 2
Next j
Range("K" & Target.Row).Value = K
End If
Application.EnableEvents = True
End Sub
 
Upvote 0
if you want select multiple cell at one row also try this:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Long, Lr As Long, j As Long, K As Double
Lr = Range("F" & Rows.Count).End(xlUp).Row
If Intersect(Target, Union(Range("C3:C" & Lr - 1), Range("F3:J" & Lr - 1), Range("K2"))) Is Nothing Then Exit Sub
Application.EnableEvents = False
Debug.Print Application.WorksheetFunction.Sum(Target)
If Not Intersect(Target, Range("K2")) Is Nothing Then
Range("F3:J" & Lr - 1).Interior.Color = 16777215
Range("K2").Copy
Range("F3:J" & Lr - 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("K3:K" & Lr - 1).ClearContents
Range("K2").Activate
End If
If Not Intersect(Target, Union(Range("C3:C" & Lr - 1), Range("F3:J" & Lr - 1))) Is Nothing Then
Target.Interior.Color = 65535
For j = 3 To 10
If Cells(Target.Row, j).Interior.Color = 65535 Then K = K + Cells(Target.Row, j).Value
If j = 3 Then j = j + 2
Next j
Range("K" & Target.Row).Value = K
End If
Application.EnableEvents = True
End Sub
 
Upvote 0
Thank for all your help. The code is working perfectly.
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

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