ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Enable / Disable Buttons (https://www.excelbanter.com/excel-programming/420624-enable-disable-buttons.html)

Sue

Enable / Disable Buttons
 
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
--
Many Thanks

Sue

Dave Peterson

Enable / Disable Buttons
 
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


Sue wrote:

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
--
Many Thanks

Sue


--

Dave Peterson

Sue

Enable / Disable Buttons
 
Hi Mr Peterson

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

Sue


"Dave Peterson" wrote:

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


Sue wrote:

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
--
Many Thanks

Sue


--

Dave Peterson



All times are GMT +1. The time now is 01:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com