Excel VBA Macros Using Checkboxes To Hide Rows - If Else Statements - excel

I have 60 check boxes on one worksheet. They are form control checkboxes. They all do a similar function which is hide rows. When you click the check box it shows the rows
When unchecked the rows are hidden. Is there an easy if else or case statement I could write for this:
Sub CheckBox1_Click()
If Range("B4").Value = True Then
Rows("5:62").EntireRow.Hidden = False
Else
Rows("5:62").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox2_Click()
If Range("B63").Value = True Then
Rows("64:102").EntireRow.Hidden = False
Else
Rows("64:102").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox3_Click()
If Range("B103").Value = True Then
Rows("104:129").EntireRow.Hidden = False
Else
Rows("104:129").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox4_Click()
If Range("B130").Value = True Then
Rows("131:160").EntireRow.Hidden = False
Else
Rows("131:160").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox5_Click()
If Range("B161").Value = True Then
Rows("162:183").EntireRow.Hidden = False
Else
Rows("162:183").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox6_Click()
If Range("B184").Value = True Then
Rows("185:222").EntireRow.Hidden = False
Else
Rows("185:222").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox7_Click()
If Range("B223").Value = True Then
Rows("224:244").EntireRow.Hidden = False
Else
Rows("224:244").EntireRow.Hidden = True
End If
End Sub
........etc etc
Also, if I need to add more rows in or delete rows, how would I code that in?
I would appreciate a format that I could use to create my own check boxes in the future without having to manually fix and edit them individually

You could use something like this:
Sub CheckBox1_Click()
HideRows "B63", "64:102"
End Sub
Sub HideRows(controllerRange, rowRange)
Rows(rowRange).EntireRow.Hidden = Not Range(controllerRange).Value
End Sub
You could also call HideRows directly from the checkbox.
In CheckBox1 assign macro, you would enter the following:
'HideRows "B63","64:102"'
Note: The single quotes MUST be entered as shown.

Related

Microsoft VBA Error "Sub or Function not defined"

I'm trying to show and hide sheets in excel using the if else syntax but i get this error in the first line
'SUB OR FUNCTION NOT DEFINED'
the code is in** sheet 3**
any help is appreciated!
Thank you/Faleminderit
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Me.Range("C1"), Target) Is Nothing Then Exit Sub
If Worksheets(Sheet3).Range(C1).Value = "INTERIM" Then
ShowHide (False)
Worksheets(Sheet1).Visible = True
Worksheets(Sheet3).Visible = True
ElseIf Worksheets(Sheet3).Range(C1) = "ESTIMATED_BUDGET" Then
ShowHide (False)
Worksheets(Sheet5).Visible = True
Worksheets(Sheet3).Visible = True
ElseIf Worksheets(Sheet3).Range(C1) = "Final" Then
ShowHide (False)
Worksheets(Sheet6).Visible = True
Worksheets(Sheet3).Visible = True
ElseIf Worksheets(Sheet3).Range(C1) = "ALL" Then
ShowHide (True)
End If
End Sub
Sub Macro1()
End Sub
I tried different ways to do it and i get the same error
When testing here I created 2 sheets so I had Sheet1, Sheet2, and Sheet 3.
I had hadded your code to the cose of sheet 1 initially and straight away was getting errors. I think it is important to note that sheet names and cell references or ranges so need to be encapsualted with Quote marks ("Sheet3").
Once the Sheets and Cells where quoted I was able to continue. I'm not sure on the purpose of "ShowHide". I will assume you wanted to track if the sheets are visible or not, this will need to be declared.
I also recommend setting 'Option Explicit' at the top of the module, this helps with error handling and debugging when you have problems.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ShowHide As Boolean
ShowHide = False
If Intersect(Me.Range("C1"), Target) Is Nothing Then Exit Sub
If Worksheets("Sheet3").Range("C1").Value = "INTERIM" Then
ShowHide = False
Worksheets("Sheet1").Visible = True
Worksheets("Sheet3").Visible = True
ElseIf Worksheets("Sheet3").Range("C1") = "ESTIMATED_BUDGET" Then
ShowHide = False
Worksheets("Sheet5").Visible = True
Worksheets("Sheet3").Visible = True
ElseIf Worksheets("Sheet3").Range("C1") = "Final" Then
ShowHide = False
Worksheets("Sheet6").Visible = True
Worksheets("Sheet3").Visible = True
ElseIf Worksheets("Sheet3").Range("C1") = "ALL" Then
ShowHide = True
End If
End Sub
This worked as I expected.

Hide and unhide rows in Excel VBA when specific value in the one cell

I want to hide/unhide if a specific value is selected from a drop down list. As long as it works, it could be under the worksheet code (when the value is selected) or when a button is pushed. Your help is greatly appreciated.
I have tried with this code unsuccessfully..
Application.EnableEvents = False
If DWR.Cells(4, 14) = "CANTI" Then
DWR.Activate
DWR.Range("10:49").EntireRow.Hidden = False
'must hide the empty rows
DWR.Activate
DWR.Range("50:89").EntireRow.Hidden = True
ElseIf DWR.Cells(4, 14) = "F100" Then
DWR.Activate
DWR.Range("50:89").EntireRow.Hidden = True
'must hide the empty rows
DWR.Activate
DWR.Range("10:49").EntireRow.Hidden = False
End If
Application.EnableEvents = True
Any suggestions?
Try something like this?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then
Application.EnableEvents = False
Range("10:89").EntireRow.Hidden = False '~~~> Default case: display ALL rows
If Target = "CANTI" Then
Range("10:49").EntireRow.Hidden = False
Range("50:89").EntireRow.Hidden = True
ElseIf Target = "F100" Then
Range("10:49").EntireRow.Hidden = True
Range("50:89").EntireRow.Hidden = False
End If
Application.EnableEvents = True
End If
End Sub

Checking one box unchecks the adjacent box in Excel

I am currently trying work on a code that can loop through all the check boxes and uncheck a box if the box next to it is checked.
Currently, I have something written out, and I know it's no where near what I need it to be. I know how to do it individually by box:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Value = False
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
CheckBox1.Value = False
End If
End Sub
However, I have several hundred lines of check boxes that I'd have to write this for so if I could just loop through the check boxes it would be a life saver!So I started out with:
Dim i As Integer
i = 1
a = 2
If CheckBoxi.Value = True Then
CheckBoxa.Value = False
End If
If CheckBoxa.Value = True Then
CheckBoxi.Value = False
End If
i = i + 2
a = a + 2
End
However this doesn't seem to work and I have no idea where to go from here. Any help would be greatly appreciated!

Excel Group of image hiding on another sheet checkbox

I'm trying to hide a bunch of images i have placed into a group via a checkbox, I can do this via the same sheet but no on the sheet the textbox is.
Sub hideimages()
If ActiveSheet.CheckBoxes("Check Box 1").Value = 1 Then
ActiveSheet.Shapes("Group 21").Visible = True
Else: ActiveSheet.Shapes("Group 21").Visible = False
End If
End Sub
But i can't seem to figure out the right syntax to get it to affect another sheet for the group I can do it for a singular image:
Sub CheckBox33_Click()
Dim obj As Shape
Set obj = Worksheets("sheet3").Shapes("picture 2")
If obj.Visible Then
obj.Visible = True
Else
obj.Visible = False
End If
How could i merge these? the ways i have tried are not happy!
Sub hidaway()
If Worksheets("sheet1").CheckBoxes("Check Box 34").Value = 1 Then
Worksheets("sheet3").group("Group 21").Visible = True
Else: Worksheets("sheet3").group("Group 21").Visible = False
End If
End Sub
Your checkbox returns True/False so you just need to feed this value to your group visible property:
Private Sub CheckBox1_Click()
ThisWorkbook.Worksheets("Sheet3").Shapes("Group 21").Visible = Me.CheckBox1.Value
End Sub

I want to disable a checkbox when another checkbox is clicked in excel

I want to click a check box and have another linked check box be disabled in Excel.
When I uncheck that particular check box, then the disabled check box should become enabled.
I tried so many things but not able to find the solution. Can I do this without a script?
I have 76 check boxes to work upon. So is this possible without a script? TIA
With two ActiveX check boxes the code would be:
Private Sub CheckBox1_Click()
If CheckBox2.Enabled = True Then
CheckBox2.Enabled = False
Else:
CheckBox2.Enabled = True
End If
End Sub
It's simple, but works.
If you want the code to check and disable clicking on the second box, use the below code. If you uncheck the initial box, it will also enable and uncheck the second one.
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Value = True
CheckBox2.Enabled = False
Else
CheckBox2.Value = False
CheckBox2.Enabled = True
End If
End Sub
more directly
Sub CheckBox1_Click()
CheckBox2.Enabled = Not CheckBox2.Enabled
End Sub
I belive you could use "OptionButton's". You don't need code for them. But in that way you'll be able to check only one of 76. And what do you mean by "linked check box"?
Option Compare Database
Private Sub Ck1_Click()
If Ck1 = True Then
Ck2.Enabled = False
Ck3.Enabled = False
Else
Ck2.Enabled = True
Ck3.Enabled = True
End If
End Sub
Private Sub Ck2_Click()
If Ck2 = True Then
Ck1.Enabled = False
Ck3.Enabled = False
Else
Ck1.Enabled = True
Ck3.Enabled = True
End If
End Sub
Private Sub Ck3_Click()
If Ck3 = True Then
Ck1.Enabled = False
Ck2.Enabled = False
Else
Ck1.Enabled = True
Ck2.Enabled = True
End If
End Sub

Resources