1

Iam trying to create a simple if statement in Excel with VBA.

I'm creating a new checkbox

Adds the following code to the box.

Sub CheckBox1_Click()
   HideRows "2:5"
End Sub

Sub HideRows(rowRange)
If CheckBox1 = False Then
   Rows(rowRange).EntireRow.Hidden = True
   Else: Rows(rowRange).EntireRow.Hidden = False
   End If
End Sub

Result: The rows are hidden both if the checkbox is checked or unchecked.

(checkbox is checked)

All rows are visible

Uncheck the checkbox

Result: All rows are hidden

(checkbox is unchecked)

All rows are visible

Uncheck the checkbox

Result: All rows are hidden

0

3 Answers 3

4

You want it in a Change Event.

You do not need the If Then. CheckBox1 rturns a TRUE/FALSE, just use that.

And the EntireRow is also not needed when refering to Rows(). You are already refering to the whole row.

Also, it is good practice to always declare the parent to any Range Object, which Rows() is. If the the code is in the Worksheet code then use Me as it will refer to itself. If the code is in a module then use ActiveSheet or more preferably the specific sheet, Worksheets("Sheet1") :

Private Sub CheckBox1_Change()
    HideRows "2:5"
End Sub



Sub HideRows(rowRange)
   'if this code is not in the worksheet code then change `Me` to `ActiveSheet`
   Me.Rows(rowRange).Hidden = Not CheckBox1

End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

Upvoted for pointing out the redundancy of the If...Then block and Boolean assignments... I'd qualify Rows with ActiveSheet and explicitly refer to CheckBox1.Value though ;-)
What he said ^^^^
@Mat'sMug though, if this is on the sheet code is it really needed or would Me be better?
Ah, yes, absolutely - I keep assuming comboboxes are on userforms :)
@Mat'sMug I'm just glad I was able to get the +1 from you. I've been trying to step up my vba answers.
2

Assuming its an ActiveX CheckBox, place this code in Sheet Module...

Private Sub CheckBox1_Click()
If CheckBox1 = True Then
    Rows("2:5").Hidden = True
Else
    Rows("2:5").Hidden = False
End If
End Sub

Edit:

Or just use this...

Private Sub CheckBox1_Click()
    Rows("2:5").Hidden = CheckBox1
End Sub

3 Comments

Why not just ActiveSheet.Rows("2:5").Hidden = CheckBox1.Value?
@Mat'sMug Yes, that's true. But it was just to demonstrate how it would work rather than finding the shortest method to achieve the end result. What you suggested actually doesn't even require to qualify it with sheet reference as the code will be sitting in the sheet module itself. :)
That's correct (+1) - the applicable explicit qualifier would be Me if that code is in a worksheet's code-behind; arguably the active sheet would/should be that sheet though, but yeah Me is semantically more appropriate than ActiveSheet... and Me is always a redundant qualifier.
1

You can put this in one sub, assuming its an activeX checkbox

Private Sub CheckBox1_Click()
If CheckBox1 = True Then
    [2:5].EntireRow.Hidden = False
    Else: [2:5].EntireRow.Hidden = True
End If
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.