I have a Workbook with multiple Sheets. I have a menu page (Worksheet) with multiple user choices (Enter a new order, update an order, etc.) Each choice has a check box beside it and depending on which check box is checked, cells F4:F21
change from 0
to 1
and, cell B1
changes to the name of the Worksheet where I want to go. I have the following VBA in the Main Menu worksheet but when I click a check box, nothing happens. Any ideas why?
CODE
Private Sub Worksheet_Activate()
ClearMenuForm
End Sub
Private Sub Worksheet_Change (ByVal Target As Range)
Dim sh As String
If Not Intersect(Target, Range("F4:F21")) Is Nothing Then
sh = Cells(1, "B").Value
Sheets(sh).Select
End If
End Sub
You've defined
sh
as aString
. Since there is no sheet named "1", for example, your code will generate a "Subscript out of Range" runtime error. Try changingsh
to aLong
.Clicking a check box does not activate the event
Worksheet_Change
(see this). That is why nothing happens.Try changing one of the cells instead to see the effect.
What I think you want to do is assign an action to your Checkbox(es). You can do this in two ways:
Right clicking on the checkbox, and Assign Macro...
You have to create the associated macro, which will likely contain parts of the code that you already wrote, and/or calls to subs you have. You may bring the VBE (Alt+F11), insert a module in your VBA project, and write your
Sub
, e.g.,Via VBA (e.g., this). With the sample code below, you would execute
InitCBs
, and that would associateCheckBox1Change
with the checkbox (it actually assigns actions for both checkboxes in the figure; action for checkbox 2 isCheckBox2Change
). You may also setInitCBs
to be executed when opening the file.