PDA

View Full Version : [SOLVED:] Hide rows when check box selected



hrzagi
03-30-2021, 10:32 AM
Can someone help me with VBA code to hide rows when checkbox is checked and unhide when its unchecked? I tried several solutions I have found online but nothing works for me :doh:

p45cal
03-31-2021, 03:13 AM
The same as http://www.vbaexpress.com/forum/showthread.php?68592-Hide-rows-with-option-buttons&p=408343&viewfull=1#post408343 but where that used OptionButtons now use CheckBoxes.

hrzagi
03-31-2021, 03:55 AM
Im not quite sure how to adapt that code


Sub hideRows ()
Set OptBtns = Me.OptionButtons
Rows("36:38").EntireRow.Hidden = Not (OptBtns("Gumb mogućnosti 57").Value = 1)
End Sub


I tried with


Sub hideRows ()
Set CheckBox = Me.CheckBoxes
Rows("36:38").EntireRow.Hidden = Not (CheckBox("Potvrdni okvir 70").Value = 1)
End Sub


but I get "Invalid use of Me keyword".

The code I found online and its working is

Sub HideUnhideCheckbox()
Rows("59:60").Select
If Selection.EntireRow.Hidden = True Then
Selection.EntireRow.Hidden = False
Else
Selection.EntireRow.Hidden = True
End If
End Sub

but that make selection of rows and I dont want that but whatever I try to change I get more and more errors.
Im very new to VBA so this all give me headache :(

p45cal
03-31-2021, 07:23 AM
but I get "Invalid use of Me keyword".

The Me word depends where the code is; if the code is in a sheet's code-module (as it was in your last thread) the Me refers to that sheet.
If the code is in the ThisWorkbook code-module then the Me refers to the workbook.

It looks like you may have put the code in a standard code-module so I guess Me wouldn't apply. So you need to replace Me with a reference to the sheet concerned, it could be
Sheets("Sheet39").Checkboxes
or
Activesheet.CheckBoxes.

So, either use the Me and put the code in the sheet's code-module (safest), but if that sheet might be deleted by the user the code will be deleted too!
or put it into a standard code-module and use a suggestion in the paragraph above:

Sub hideRows2 ()
Set CheckBoxs = ActiveSheet.CheckBoxes
ActiveSheet.Rows("36:38").EntireRow.Hidden = Not (CheckBoxs("Potvrdni okvir 70").Value = 1)
End Sub
and depending on whether you want to see or hide the relevant rows you include/miss out the Not.

hrzagi
03-31-2021, 10:54 AM
Oh, I only needed to put code in sheet code module. Users cant delete sheets so that works for me. Thanx :thumb