Checkbox to Hide/Unhide Row Not Unhiding - excel

I have a form control checkbox and the following code to hide and unhide row 10.
It hides but does not unhide.
Sub CheckBox1_Click()
If Range("C84").Value = True Then
Rows("10:10").EntireRow.Hidden = False
Else
Rows("10:10").EntireRow.Hidden = True
End If
End Sub

The code works perfectly.
Furthermore, if the value of cell C84 is not used elsewhere, the same can be obtained without using LinkedCell:
Sub CheckBox1_Click()
Rows("10:10").EntireRow.Hidden = Not CheckBox1
End Sub

For it to unhide c84 value should be false as long as it is true it will be hidden only

Related

Code only works when I click on a cell in the sheet?

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

Auto Select Checkbox triggered by another checkbox

I am trying to auto select a checkbox if any another checkbox is selected. All this checkboxes are on the same sheet and basically i want check box 7 to tick if checkbox 3,4 or 5 is selected.
Private Sub Worksheet_Change(ByVal Target As Range)
If Sheets("Start Page").CheckBox3 = True Or Sheets("Start Page").CheckBox4 = True Or
Sheets("Start Page").CheckBox5 = True Then
Sheets("Start Page").CheckBox7 = True
Else
End If
End Sub
Any help would be greatly appreciated.
Even if your checkboxes are linked to a cell (LinkedCell-property), the Worksheet-Change-event is not triggered when you click on a Checkbox.
You need to catch the Click-Event of the checkboxes. For every checkbox, put a Click-event routine into the sheet module. To prevent that the logic if or if not to set the "calculated" checkbox is repeated, let those event handler call a common routine that does the calculation.
Private Sub CheckBox3_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox4_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox5_Click()
Call SetMyCheckBox
End Sub
Sub SetMyCheckBox()
Me.CheckBox7.Value = Me.CheckBox3.Value Or Me.CheckBox4.Value Or Me.CheckBox5.Value
End Sub
You should, by the way, consider to give your checkboxes more meaningful names.

How can I assign a dynamic range (of rows) to a checkbox in vba?

I'm creating a workbook in excel and using activeX checkboxes to hide/unhide some rows each time as a way to enable or disable them. For that I use VBA code. However in that code I specify a static range of rows. The problem is that if I insert a new row, everything is now offset and i have to manually rewrite all the intervals. Is there a way to do this dynamically.
Here is the code I use for two consecutive checkboxes:
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
[24:41].EntireRow.Hidden = False
Else: [24:41].EntireRow.Hidden = True
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2 = True Then
[42:49].EntireRow.Hidden = False
Else: [42:49].EntireRow.Hidden = True
End If
End Sub
I have searched for a while before asking the question, and I apologise if there is an answer somewhere, I'm a total newbie to excel and VBA and I don't think I can adapt a solution that is remotely similar to the problem I'm facing.
Thanks for your precious help in advance.
Have a Column in which you write an X in each row you want to hide / unhide.
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = False
End If
Next c
Else
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = True
End If
Next c
End If
End Sub
You could try naming your range
thisworkbook.names.add name:="mydinamicrange" ,ReferstoR1C1:="Sheet1!R24c1:R49C10"
Then , you may use it with something like this :
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden = False
Else: thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden= True
End If

Linked checkboxes in Userform and Worksheet

I have a table format questionnaire to be filled on Userform. There are some sections that to be answered only by check-boxes.Those check-boxes are exist in both worksheet and userform. When the user click the box in the userform it need to be ticked in worksheet as well(I don't know if it is possible).
You can write some If statements to assign the same value to the worksheet checkbox on the click or change event of the UserForm checkbox.
Something like:
Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
ThisWorkbook.Sheets(1).CheckBox1.Value = True
ElseIf Me.CheckBox1.Value = False Then
ThisWorkbook.Sheets(1).CheckBox1.Value = False
End If
End Sub
1) Link your checkboxes on your sheet to a cell (whichever cell, even on another sheet)
2) Here is the code you could alter to your liking. Make sure to use it as a clickevent on your checkboxes on your userform:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then Range("D3").Value = True
If CheckBox1.Value = False Then Range("D3").Value = False
End Sub
3) Outcome will be like:

VBA expand/collapse rows with same macro button

We have a trivial problem regarding how to run a simple macro button. The purpose of this button is two-fold: expanding a row and collapsing a row.
1 on pressing the button this VBA command is initiated:
Sub Macro7()
Rows(7).ShowDetail = True
End Sub
This command expands row 7.
2 on pressing the button again (whilst the row is expanded), this VBA is initiated:
Sub Macro7()
Rows(7).ShowDetail = False
End Sub
This collapses the row.
Is there a way to link a button to two macros?
Thanks in advance!!!
M
Sub Macro7()
With Rows(7)
.ShowDetail = Not .ShowDetail
End With
End Sub
No need to. Just adjust your macro to check the current state of your row (collapsed or expanded) and act accordingly:
Sub ExpandOrCollapse()
Rows(7).ShowDetail=IIF(Rows(7).ShowDetail,False,true)
End Sub
I tried above answers and it didn't work for me. Below is the code that works:
Sub rowExpanded()
Rows("7:7").Select
Selection.EntireRow.Hidden = IIf(Selection.EntireRow.Hidden, False, True)
End Sub
Try this
Dim rowExpanded As Boolean
rowExpanded = Rows(7).ShowDetail
If rowExpanded = True Then
Rows(7).ShowDetail = False
Else
Rows(7).ShowDetail = True
End If
Try using a Command Button (ActiveX Control). Use the button caption to identify the toggle state.
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "-" Then
ActiveSheet.Outline.ShowLevels Rowlevels:=1, ColumnLevels:=1
JobDescriptionToggleButton.Caption = "+"
Else
ActiveSheet.Outline.ShowLevels Rowlevels:=8, ColumnLevels:=8
JobDescriptionToggleButton.Caption = "-"
End If
End Sub

Resources