Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 201
Default Enable/Disable Command Button

I am hoping someone might be able to point me in the right direction
regarding a feature I have no idea what syntax to use, or where to place it.

I have a form created in VBE. Within this Form is a Frame that has three
Check Boxes. Also on the Form, but not in the Frame, I have a Command Button.

I want the Command Button to be disabled unless all three Check Boxes are
ticked.

Any help would be very gratefully received.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Enable/Disable Command Button

Hi Phil

The code below should do what you want, to test I set up a userform
with a frame containing the three checkboxes then added a button.
what the code does is it has a counter that you add one to if a
checkbox is checked and subtract one if the checkbox is blank, ten
when the counter reaches three the button will be enabled.

Option Explicit
Dim Ctrl As MSForms.Control
Dim i, CountChks As Integer

Private Sub CheckBox1_Click()

i = 1

CheckYourChecks

End Sub
Private Sub CheckBox2_Click()

i = 2

CheckYourChecks

End Sub
Private Sub CheckBox3_Click()

i = 3

CheckYourChecks

End Sub

Private Sub UserForm_Initialize()

i = 0

CountChks = 0

CommandButton1.Enabled = False

End Sub

Sub CheckYourChecks()

Set Ctrl = UserForm1.Controls("CheckBox" & i)

If Ctrl.Value = True Then

CountChks = CountChks + 1

Else

CountChks = CountChks - 1

End If

If CountChks = 3 Then

CommandButton1.Enabled = True

Else

CommandButton1.Enabled = False

End If

End Sub


Hope this helps

S

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Enable/Disable Command Button

You can use a bit flag, so if you need to vary the number of check boxes, it
is more easy to maintain.
Whilst this code is not the most compact, it shows the logic of the
procedure clearly.

Dim Flag As Long
Const ALLSET As Long = 2 ^ 0 + 2 ^ 1 + 2 ^ 2

Private Sub CheckBox1_Click()

If CheckBox1.Value = True Then
Flag = Flag Or 2 ^ 0
Else
Flag = Flag And Not (2 ^ 0)
End If

Call CheckStatus

End Sub

Private Sub CheckBox2_Click()

If CheckBox2.Value = True Then
Flag = Flag Or 2 ^ 1
Else
Flag = Flag And Not (2 ^ 1)
End If

Call CheckStatus

End Sub

Private Sub CheckBox3_Click()

If CheckBox3.Value = True Then
Flag = Flag Or 2 ^ 2
Else
Flag = Flag And Not (2 ^ 2)
End If

Call CheckStatus

End Sub

Private Sub CheckStatus()
CommandButton2.Enabled = ((Flag And ALLSET) = ALLSET)
End Sub

Private Sub UserForm_Initialize()
Flag = 0
Call CheckStatus
End Sub

NickHK

"Phil" wrote in message
...
I am hoping someone might be able to point me in the right direction
regarding a feature I have no idea what syntax to use, or where to place

it.

I have a form created in VBE. Within this Form is a Frame that has three
Check Boxes. Also on the Form, but not in the Frame, I have a Command

Button.

I want the Command Button to be disabled unless all three Check Boxes are
ticked.

Any help would be very gratefully received.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 201
Default Enable/Disable Command Button

Many Thanks to you both for your help. It is much appreciated.

"NickHK" wrote:

You can use a bit flag, so if you need to vary the number of check boxes, it
is more easy to maintain.
Whilst this code is not the most compact, it shows the logic of the
procedure clearly.

Dim Flag As Long
Const ALLSET As Long = 2 ^ 0 + 2 ^ 1 + 2 ^ 2

Private Sub CheckBox1_Click()

If CheckBox1.Value = True Then
Flag = Flag Or 2 ^ 0
Else
Flag = Flag And Not (2 ^ 0)
End If

Call CheckStatus

End Sub

Private Sub CheckBox2_Click()

If CheckBox2.Value = True Then
Flag = Flag Or 2 ^ 1
Else
Flag = Flag And Not (2 ^ 1)
End If

Call CheckStatus

End Sub

Private Sub CheckBox3_Click()

If CheckBox3.Value = True Then
Flag = Flag Or 2 ^ 2
Else
Flag = Flag And Not (2 ^ 2)
End If

Call CheckStatus

End Sub

Private Sub CheckStatus()
CommandButton2.Enabled = ((Flag And ALLSET) = ALLSET)
End Sub

Private Sub UserForm_Initialize()
Flag = 0
Call CheckStatus
End Sub

NickHK

"Phil" wrote in message
...
I am hoping someone might be able to point me in the right direction
regarding a feature I have no idea what syntax to use, or where to place

it.

I have a form created in VBE. Within this Form is a Frame that has three
Check Boxes. Also on the Form, but not in the Frame, I have a Command

Button.

I want the Command Button to be disabled unless all three Check Boxes are
ticked.

Any help would be very gratefully received.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to disable/enable command button(ActiveX) using macro enahs_naneek Excel Discussion (Misc queries) 1 February 5th 10 12:32 PM
enable/disable button based on cell value Shoney Excel Discussion (Misc queries) 3 January 9th 08 07:34 PM
Enable / Disable a button using a macro SapnaT Excel Programming 0 November 26th 04 09:56 AM
How to enable/disable a button. Polly[_3_] Excel Programming 2 May 28th 04 04:24 AM
Enable/Disable Button surplusbc[_6_] Excel Programming 1 February 1st 04 09:53 PM


All times are GMT +1. The time now is 11:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"