Run Worksheet code automatically

LabLady11

New Member
Joined
Oct 22, 2021
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I am running the code below in a worksheet but have to double click on the cell in order for it to run. Can this run automatically?

Private Sub Worksheet_Change(ByVal Target As Range)
MyVal = Range("A1").Text


With ActiveSheet.Tab
Select Case MyVal
Case "1"
.Color = vbRed
Case "0"
.ColorIndex = xlColorIndexNone
Case Else
.ColorIndex = xlColorIndexNone
End Select
End With
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Does A1 have a formula in it?
 
Upvote 0
In that case try
VBA Code:
Private Sub Worksheet_Calculate()
   Select Case Range("A1").Value
      Case 1
         Me.Tab.Color = vbRed
      Case Else
         Me.Tab.ColorIndex = xlColorIndexNone
   End Select
End Sub
Although this will run when any cell in the sheet is calculated.
 
Upvote 0
In that case try
VBA Code:
Private Sub Worksheet_Calculate()
   Select Case Range("A1").Value
      Case 1
         Me.Tab.Color = vbRed
      Case Else
         Me.Tab.ColorIndex = xlColorIndexNone
   End Select
End Sub
Although this will run when any cell in the sheet is calculated.
THANK YOU SO MUCH!!! IT WORKS :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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