CheckBox code

K

Khalil Handal

Hello to everybody,
I have a sheet for showing a certificate of marks. Sometimes Ithe sum of the
marks need to be shown when printing the certificate, and sometime I n don't
need to see the sum.
I have a checkBox named chekcbox1 on the sheet. What I need is a VBA code so
that when the box is checked to hide the contents of the range C34:E34 which
containes the sum of the marks.
PS. by "hiding" I mean "not to be seen. (maybe changing their color to
white!!)
Can anyone help in writing the code for me?
 
G

Guest

hi
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Range("C34:E34").Font.Color = RGB(255, 255, 255) ' white
Else
Range("C34:E34").Font.Color = RGB(0, 0, 0) 'black
End If
End Sub

regards
FSt1
 
K

Khalil Handal

Thanks.It worked well

FSt1 said:
hi
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Range("C34:E34").Font.Color = RGB(255, 255, 255) ' white
Else
Range("C34:E34").Font.Color = RGB(0, 0, 0) 'black
End If
End Sub

regards
FSt1
 
K

Khalil Handal

Hi,
My sheet is protected and the cells are protected.
It does not work in this case what should I do????
 
G

Gord Dibben

Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
ActiveSheet.UnProtect
Range("C34:E34").Font.Color = RGB(255, 255, 255) ' white
Else
Range("C34:E34").Font.Color = RGB(0, 0, 0) 'black
End If
ActiveSheet.Protect
End Sub


Gord Dibben MS Excel MVP
 
K

Khalil Handal

Hi,
When I clicked the box the first time it worked fine.
Then I clicked it again (a second time) and had the following error message:
Unable to set the color property of the font class
any ideas!!!!
 
R

Rick Rothstein \(MVP - VB\)

Even more descriptive (in my opinion) are these (which use two of the many,
many predefined constants in VBA)...
Range("C34:E34").Font.Color = RGB(255, 255, 255) ' white

Range("C34:E34").Font.Color = vbWhite
Range("C34:E34").Font.Color = RGB(0, 0, 0) 'black

Range("C34:E34").Font.Color = vbBlack


Rick
 
D

Dave Peterson

Moving the .unprotect line helped:

Option Explicit
Private Sub CheckBox1_Click()
Me.Unprotect
If CheckBox1.Value = True Then
Me.Range("C34:E34").Font.Color = RGB(255, 255, 255) ' white
Else
Me.Range("C34:E34").Font.Color = RGB(0, 0, 0) 'black
End If
Me.Protect
End Sub

The me keyword refers to the worksheet that owns the code.
 
K

Khalil Handal

Thanks a lot to all of you.
It worked just fine

Dave Peterson said:
Moving the .unprotect line helped:

Option Explicit
Private Sub CheckBox1_Click()
Me.Unprotect
If CheckBox1.Value = True Then
Me.Range("C34:E34").Font.Color = RGB(255, 255, 255) ' white
Else
Me.Range("C34:E34").Font.Color = RGB(0, 0, 0) 'black
End If
Me.Protect
End Sub

The me keyword refers to the worksheet that owns the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Average 6
checkbox problem 22
SUMIF Problem - Help please 2
Borders 7
sum 1
CheckBox 1
Excel sent as email has caused problem 1
***Help on writing a vba code 12

Top