2 Replies - 5765 Views - Last Post: 10 January 2008 - 03:07 PM Rate Topic: -----

#1 alien_sporez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-January 08

Excel VB Code: Check/un-check to make sheet Visible

Posted 10 January 2008 - 09:11 AM

It's been over a decade since I've done any VB coding, so I've completely lost my chops.

Anyways, I'm creating an Excel spreadsheet with ActiveX controls. It should be fairly easy, right? Not so much.

Ok, so, I have a check box named "CheckBox1" and what I would like to happen is that when the checkbox is checked, Sheet #2 is Visible. When the checkbox is un-checked, Sheet #2 becomes hidden.

With the code below, my sheet will be hidden, but un-checking the box does not make the sheet visible again. Am I missing something? It should be a simple if/then/else statement, right?

Thanks in advance.

Private Sub CheckBox1_Click()

If (CheckBox1 = 0) Then
Sheet2.Visible = False
Else
If (CheckBox1 = 1) Then
Sheet2.Visible = True
End If
End If
End Sub 


Is This A Good Question/Topic? 0
  • +

Replies To: Excel VB Code: Check/un-check to make sheet Visible

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Excel VB Code: Check/un-check to make sheet Visible

Posted 10 January 2008 - 10:39 AM

Remove the second IF statement, you only need one IF/Else. You only have two possibilities, either it is checked or it is not checked.
Private Sub CheckBox1_Click()

If (CheckBox1 = 0) Then
Sheet2.Visible = False
Else
Sheet2.Visible = True
End If
End Sub

Was This Post Helpful? 0
  • +
  • -

#3 alien_sporez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-January 08

Re: Excel VB Code: Check/un-check to make sheet Visible

Posted 10 January 2008 - 03:07 PM

Nevermind... I figured it out!

Turns out the value for the checkbox shouldn't have been "1" if it's checked, it should have been "True"

Private Sub CheckBox1_Click()

If (CheckBox1 = True) Then
Sheet2.Visible = xlSheetVisible

Else
If (CheckBox1 = False) Then
Sheet2.Visible = xlSheetHidden

End If
End If
End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1