Automatically Update Based on Change to Cell Value via Formula

jl815

New Member
Joined
Aug 25, 2020
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
Hi Guys-

I'm new to VBA and in need of your help updating this code. This code works:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then
Select Case Target.Value
Case "In Progress"
Me.Tab.Color = vbRed
Case "Closed"
Me.Tab.Color = vbGreen
Case "Open"
Me.Tab.Color = vbYellow
Case Else
Me.Tab.Color = vbBlue
End Select
End If
End Sub


However when I combine it with this one in order to have it automatically run whenever cell B3 is updated (the value of B3 comes from a vlookup formula), I get an error message "Compile Error: Syntax Error"
---------

Private Sub Worksheet_Calculate()
Static oldval
If Range("$B$3").Value <> oldval Then
oldval = Range("$B$3").Value
'
Worksheet_Change(ByVal Target As Range)

If Target.Address = "$B$3" Then
Select Case Target.Value
Case "In Progress"
Me.Tab.Color = vbRed
Case "Closed"
Me.Tab.Color = vbGreen
Case "Open"
Me.Tab.Color = vbYellow
Case Else
Me.Tab.Color = vbBlue
End Select
End If
'
End If
End Sub



Thanks in advance!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Welcome to MrExcel

Here is a simpler method which I think will work for you
Delete both subs and replace with one macro below
VBA Code:
Private Sub Worksheet_Calculate()
    Select Case Range("B3").Value
        Case "In Progress"
            Me.Tab.Color = vbRed
        Case "Closed"
            Me.Tab.Color = vbGreen
        Case "Open"
            Me.Tab.Color = vbYellow
        Case Else
            Me.Tab.Color = vbBlue
    End Select
End Sub


-------------------------------------------------------------------------------------------------------------------------------------------------------------------

For future posts
please click on icon <vba/> and paste code inside code tags (does not allow any formatting however) - looks like above


To highlight something in your code with colours or bold or italics, then click on<rich/> and paste code inside code tags - looks like below
Rich (BB code):
Private Sub Worksheet_Calculate()
    Select Case Range("B3").Value
        Case "In Progress"
            Me.Tab.Color = vbRed
        Case "Closed"
            Me.Tab.Color = vbGreen
        Case "Open"
            Me.Tab.Color = vbYellow
        Case Else
            Me.Tab.Color = vbBlue
    End Select
 
Last edited:
Upvote 0
Thank you so much! This works perfectly! I'll take your advice for future posts.
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,948
Members
449,198
Latest member
MhammadishaqKhan

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