Enable / Disable Buttons

S

Sue

Hi

On a userform I have 3 commandbuttons - say CB1, CB2,CB3 all send info to
different sheets however I'm slipping up sometimes by clicking on the wrong
button and sending the info to the wrong sheet is it possible by using a
Checkbox to use CB1 and Disable CB2 & CB3 and like wise when necessary Enable
CB2 and Disable CB1 and CB3
 
D

Dave Peterson

You could use 3 checkboxes to enable/disable each of the 3 commandbuttons--or
you could use 3 optionbuttons and choose the one checkbox that you want enabled.

Or you could just add a prompt to the 3 procedures that do the work:

Dim Resp as long
resp = inputbox(Prompt:="Are you sure you want to use CB1",buttons:=vbyesno)
if resp = vbno then
exit sub
end if

====
But if you want...

I create a small userform with a frame, 3 optionbuttons in that frame and 3
commandbuttons.

The frame was named: Frame1
The Optionbuttons were named: OptionButton1, OptionButton2, OptionButton3
The Commandbuttons were named: Commandbutton1, Commandbutton2, commandbutton3

This was the code I used:

Option Explicit
Private Sub OptionButton1_Click()
Call DoTheWork(WhichOne:=1)
End Sub
Private Sub OptionButton2_Click()
Call DoTheWork(WhichOne:=2)
End Sub
Private Sub OptionButton3_Click()
Call DoTheWork(WhichOne:=3)
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
With Me
For iCtr = 1 To 3
.Controls("Optionbutton" & iCtr).Value = False
.Controls("Commandbutton" & iCtr).Enabled = False
Next iCtr
.Frame1.Caption = "Choose The Commandbutton"
End With
End Sub
Private Sub DoTheWork(WhichOne As Long)
Dim iCtr As Long
For iCtr = 1 To 3
Me.Controls("Commandbutton" & iCtr).Enabled = CBool(iCtr = WhichOne)
Next iCtr
End Sub
 
S

Sue

Hi Mr Peterson

Thank you for your help I used your Frame Solution to good effect works
really well
 

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

Top