I have VBA code which checks the value of a cell and should enable/disable a checkbox depending on the value:
Private Sub CheckBox2_Click()
If Range("A4").Value = "ZJ3" Then
CheckBox2.Enabled = True
ElseIf Range("A4").Value = "ZJ2" Then
CheckBox2.Enabled = False
CheckBox2.Value = False
End If
End Sub
When I select ZJ2 in cell A4, the checkbox does what is intended, and disables the value and the entry of the checkbox.
When I change the value of ZJ3 it remains greyed out when it should enable.
It might be better to use the Worksheet_Change event to control whether the checkbox is enabled or not, something like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A4")) Is Nothing Then Exit Sub
Select Case Me.Range("A4").Value
Case "ZJ3"
Me.CheckBox2.Enabled = True
Case "ZJ2"
Me.CheckBox2.Enabled = False
Me.CheckBox2.Value = False
End Select
End Sub
Related
I used checkboxes, when clicked depending on which checkbox is selected, specific rows unhide.
Code runs fine. Only issue is that code is not triggered by clicking the checkbox but works when I select any cell in the sheet.
Below is some of the code used:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Activate
If Range("C2").Value Or Range("C3").Value Or Range("C4").Value Or Range("C5").Value Or Range("C6").Value Or Range("C7").Value Or Range("C8").Value Or Range("C9").Value Then
Rows("39:52").EntireRow.Hidden = False
Rows("166:169").EntireRow.Hidden = False
Rows("173:175").EntireRow.Hidden = False
Else
Rows("39:52").EntireRow.Hidden = True
Rows("166:169").EntireRow.Hidden = True
Rows("173:175").EntireRow.Hidden = True
End If
End Sub
Because you using event [Worksheet_SelectionChange], so it only run when you change selection cell.
If you want run when you click checkbox, you must write event [click] of checkbox
Ex:
Sub CheckBox1_Click()
End Sub
I have two userforms in a worksheet that open when yes or no are respectively selected from a dropdown list in cell G5.
Everytime a user continues to enter data somwhere else in the worksheet (in cells other than G5), the userform reopens/reappears though.
Is there a way to ensure the userform is only opened when the value in G5 changes?
(Application.EnableEvents = True needs to be on as there are more userforms further down the sheet.)
Thanks in advance for any help!
Here's my code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("G5")
Application.EnableEvents = False
If Target = "No" Then
Form1.Show
ElseIf Target = "Yes" Then
From2.Show
End If
Application.EnableEvents = True
End Sub
Something like
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Address = "$G$5" Then
If Target = "No" Then
Form1.Show
ElseIf Target = "Yes" Then
From2.Show
End If
End If
Application.EnableEvents = True
End Sub
I am quite new programming macros in Excel and I am not sure how to initialize a Static variable, initialize it once, and then use it keeping the value when I modify it during different function calls.
I have a cell with the caption "Hide rows", and I want to toggle the visualization of different rows when clicking that cell.
I tried with this code, but like I initialize the variable Hidden to False at the beginning, it only works hidding the rows, not showing them.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static Hidden As Boolean
Hidden = False
If Selection.Count = 1 Then
If Not Intersect(Target, Range("F8")) Is Nothing Then
If Hidden = False Then
Rows("1:1").EntireRow.Hidden = True
MsgBox "Aaaau!"
Hidden = True
Else
Rows("1:1").EntireRow.Hidden = False
MsgBox "Hola!"
Hidden = False
End If
End If
End If
End Sub
you set hidden to false at the beginning of the macro, so hidden is always false when entering the test instruction.
try this
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static Hidden As Boolean
If Selection.Count = 1 Then
If Not Intersect(Target, Range("F8")) Is Nothing Then
If Hidden = False Then
Rows("1:1").EntireRow.Hidden = True
MsgBox "Aaaau!"
Hidden = True
Else
Rows("1:1").EntireRow.Hidden = False
MsgBox "Hola!"
Hidden = False
End If
End If
End If
End Sub
I have 2 DropDowns on an excel sheet which work independently of each other. The selections on both drop down's can cause confusion during reporting generation. So I need to set DropDown#1 to "select" if DropDown2 is being used by the user and vice versa.
I am trying to use the DropDown.Text property but it does not do the trick.
Sub PDropDown_Click()
Dim DropDownP As DropDown
Dim DropDownD As DropDown
Set DropDownD = Me.DropDowns("DDropDown")
Set DropDownP = Me.DropDowns("PDropDown")
DropDownD.Text ="Select"
DropDownP.Text = DropDownP.List(DropDownP.ListIndex)
Call Report_Generator.Create_Graph(DropDownP.List(DropDownP.ListIndex))
End Sub
You will have to add the value select to you combo boxes.
Dim bExit as Boolean
Private Sub ComboBox1_Change()
If bExit = True Then
bExit = False
Exit Sub
End If
bExit = True
ComboBox2.Text = "Select"
End Sub
Private Sub ComboBox2_Change()
If bExit = True Then
bExit = False
Exit Sub
End If
bExit = True
ComboBox1.Text = "Select"
End Sub
I have a macro that is supposed to hide a row in excel when a value of a given cell is "ODD" (the word, not an odd number). I've tried two different formats; neither gives any visible error but neither hides the row.
Sub Worksheet_Change(ByVal target As Range)
If target.Address <> "$B$2" Then Exit Sub
ElseIf Range("B2").Value = "ODD" Then
Rows("5:5").EntireRow.Hidden = False
Else
Rows("5:5").EntireRow.Hidden = True
End If
End If
End Sub
The other code I had is:
Select Case Range("B2").Value
Case Is = "ODD": Rows("5:5").EntireRow.Hidden = False
Case Else: Rows("5:5").EntireRow.Hidden = True
End Select
It was modified from a more advanced case statement and I just left it that way at first.
The Rows("5:5") would be better as Rows(5). The method you used would be better as Range("5:5").
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then
Rows(5).EntireRow.Hidden = (UCase(Target.Value) = "ODD")
End If
End Sub
Since comparing B2 to ODD already produces a True or False, you can dispense with the If/Else/End If. Text comparisons in VBA are usually case sensitive, hence the need for UCase to force case insensitivity.
You are missing a key code line If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then Try the following
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Set cell = Range("B2")
If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then
If Range("B2").Value = "ODD" Then
Rows("5:5").EntireRow.Hidden = False
Else
Rows("5:5").EntireRow.Hidden = True
End If
End If
End Sub
First make sure your Change Sub is stored in the Worksheet module of the Sheet you want this to perform on. Then you have a slight syntax error with your If Statements:
Private Sub Worksheet_Change(ByVal target As Range)
If target is Nothing Then Exit Sub
If target.Address <> "$B$2" Then Exit Sub
If Range("B2").Value = "ODD" Then
Rows("5:5").EntireRow.Hidden = True
Else
Rows("5:5").EntireRow.Hidden = False
End If
End Sub
When you put the If...Then... on one line, it actually closes the If (no End If needed) Also, I flipped your True and False statements to match your requirement in your question.