How can I make VB Code run when cell is changed by a formula?

babyg

New Member
Joined
Feb 27, 2009
Messages
24
I have code that runs 4 macros to hide and unhide sheets when the value of a cell changes. I have it set up so that when a user selects a value from the listbox a formula in a hidden cell changes to either a 1 or 0 depending on the value selected. The problem is unless I manually click on the formula cell the code doesn't run. How can I automate it so that my code recognizes the value in the cell changed without having to double click it?

Here is the code I am using:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Cells.Count > 1 Then Exit Sub
If IsNumeric(Target) And Target.Address = "$A$20" Then
Select Case Target.Value
Case Is = 0
Run "ShowAdminSheets"
Run "HideUserSheets"
Case Is = 1
Run "HideAdminSheets"
Run "ShowUserSheets"
End Select
End If
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

NeonRedSharpie

Well-known Member
Joined
Jul 14, 2014
Messages
1,678
Change it to:

Code:
  Private Sub Worksheet_Change(ByVal Target As Excel.Range)If Target.Cells.Count > 1 Then Exit Sub
Select Case Range(A20).value
Case Is = 0
Run "ShowAdminSheets"
Run "HideUserSheets"
Case Is = 1
Run "HideAdminSheets"
Run "ShowUserSheets"
End Select
End If
End Sub
 
Upvote 0

babyg

New Member
Joined
Feb 27, 2009
Messages
24
That doesn't seem to work either. It is still not recognizing it until I double click on it. Also when I double click now it is returning a compile error message.
 
Upvote 0

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
You can use the Calculate event, which will fire when the sheet calculates. Just note that it doesn't support the target argument.
 
Upvote 0

babyg

New Member
Joined
Feb 27, 2009
Messages
24
ADVERTISEMENT
Can you please give me an idea of what that would look like? I'm sorry I'm new to VB.
 
Upvote 0

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Using what was already posted:

Code:
Private Sub Worksheet_Calculate()
  Select Case Range("A20").Value
    Case Is = 0
      Run "ShowAdminSheets"
      Run "HideUserSheets"
    Case Is = 1
      Run "HideAdminSheets"
      Run "ShowUserSheets"
  End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,196,013
Messages
6,012,857
Members
441,736
Latest member
Tkpmm

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
Top