ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using Checkboxes (https://www.excelbanter.com/excel-programming/380021-using-checkboxes.html)

freddy

Using Checkboxes
 
I'd like to create a userform containing multiple checkboxes that, after the
user enables the ones selected, will run a corresponding VB macro. For
example, I'd like to create a checklist of customer names, the user selects
the ones to be processed, then the user would click a "Go" button, then all
of the selected customer names would invoke a corresponding but different VB
macro. (Customer1 would run macro1, Customer2 would run macro2... not macro1,
etc.) Does anyone have sample code I can refer to?

John Bundy

Using Checkboxes
 
There are various ways, the easiest is probably to insert a new module and
place your code there, such as
Sub Customer1()
MsgBox "Customer Selected"
End Sub

then in the code on the sheet you can call that sub
Call Customer1

then messagebox will appear.
Now just check to see if each is selected and call the appropriate sub routine
--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Freddy" wrote:

I'd like to create a userform containing multiple checkboxes that, after the
user enables the ones selected, will run a corresponding VB macro. For
example, I'd like to create a checklist of customer names, the user selects
the ones to be processed, then the user would click a "Go" button, then all
of the selected customer names would invoke a corresponding but different VB
macro. (Customer1 would run macro1, Customer2 would run macro2... not macro1,
etc.) Does anyone have sample code I can refer to?


freddy

Using Checkboxes
 
I wrote sample code as per your example, however, after Sub Customer1() is
done how do I direct the code back to check the value of the next checkbox
and so on? Here is an outline of what I've done so far:

Private Sub UserForm_Initialize()

CheckBox1.Caption = "Customer1"
CheckBox2.Caption = "Customer2"
CheckBox3.Caption = "Customer3"

End Sub

Private Sub CommandButton1_Click()

If CheckBox1.Value = True Then Call SubCustomer1
If CheckBox2.Value = True Then Call SubCustomer2
If CheckBox3.Value = True Then Call SubCustomer3

End Sub

Sub Customer1()
My code€¦
End Sub

Sub Customer2()
My code€¦
End Sub

Sub Customer3()
My code€¦
End Sub


"John Bundy" wrote:

There are various ways, the easiest is probably to insert a new module and
place your code there, such as
Sub Customer1()
MsgBox "Customer Selected"
End Sub

then in the code on the sheet you can call that sub
Call Customer1

then messagebox will appear.
Now just check to see if each is selected and call the appropriate sub routine
--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Freddy" wrote:

I'd like to create a userform containing multiple checkboxes that, after the
user enables the ones selected, will run a corresponding VB macro. For
example, I'd like to create a checklist of customer names, the user selects
the ones to be processed, then the user would click a "Go" button, then all
of the selected customer names would invoke a corresponding but different VB
macro. (Customer1 would run macro1, Customer2 would run macro2... not macro1,
etc.) Does anyone have sample code I can refer to?


freddy

Using Checkboxes
 
You were not repeating. Do you have sample code to make true/make false all
checkboxes using one button [Option/Toggle/Command]? Separately the code was
fine using 2 different Option buttons.

"John Bundy" wrote:

The reply box came up again so I hope I'm not repeating.
This notifies user if all boxes are unchecked:

Private Sub CommandButton1_Click()
Dim ctrl As Control
Dim allOff As Boolean

allOff = True

For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl.Value = True Then allOff = False
End If
Next ctrl
If allOff = True Then MsgBox "All boxes Unchecked"
End Sub

This will check all boxes, change true to false to uncheck them:
Private Sub CommandButton1_Click()
Dim ctrl As Control

For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
ctrl.Value = True
End If
Next ctrl
End Sub

--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Freddy" wrote:

I found that it did so automatically even before your response. Nevertheless,
your input was very helpful. Additionally, do you have sample code for the
following:

To evaluate each checkbox then, if all values are False, notify the user (I
know I can do this part via a msgbox, but not the evaluation part)?
Enable/make True all checkboxes at once?
Disable/make False all checkboxes at once?

"John Bundy" wrote:

It does so Automatically the way you have it written. Might be something to
do with how you built it. I put 3 checkboxes on a userform and a button. the
code for that button follows:

Private Sub CommandButton1_Click()
If CheckBox1.Value = True Then Call customer1
If CheckBox2.Value = True Then Call customer2
If CheckBox3.Value = True Then Call customer3
End Sub

and my subs are as follows, each in a seperate module (my preference)
Sub customer1()
Cells(1, 1) = "Customer1"

End Sub

Sub customer1()
Cells(1, 1) = "Customer1"

End Sub

Sub customer3()
Cells(3, 1) = "Customer3"

End Sub

btw I am using Excel 2003, i'm not an expert on what each are able to do but
this should be no problem.

--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Freddy" wrote:

I wrote sample code as per your example, however, after Sub Customer1() is
done how do I direct the code back to check the value of the next checkbox
and so on? Here is an outline of what I've done so far:

Private Sub UserForm_Initialize()

CheckBox1.Caption = "Customer1"
CheckBox2.Caption = "Customer2"
CheckBox3.Caption = "Customer3"

End Sub

Private Sub CommandButton1_Click()

If CheckBox1.Value = True Then Call SubCustomer1
If CheckBox2.Value = True Then Call SubCustomer2
If CheckBox3.Value = True Then Call SubCustomer3

End Sub

Sub Customer1()
My code€¦
End Sub

Sub Customer2()
My code€¦
End Sub

Sub Customer3()
My code€¦
End Sub


"John Bundy" wrote:

There are various ways, the easiest is probably to insert a new module and
place your code there, such as
Sub Customer1()
MsgBox "Customer Selected"
End Sub

then in the code on the sheet you can call that sub
Call Customer1

then messagebox will appear.
Now just check to see if each is selected and call the appropriate sub routine
--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Freddy" wrote:

I'd like to create a userform containing multiple checkboxes that, after the
user enables the ones selected, will run a corresponding VB macro. For
example, I'd like to create a checklist of customer names, the user selects
the ones to be processed, then the user would click a "Go" button, then all
of the selected customer names would invoke a corresponding but different VB
macro. (Customer1 would run macro1, Customer2 would run macro2... not macro1,
etc.) Does anyone have sample code I can refer to?


freddy

Using Checkboxes
 
I am breaking for lunch now but stay tuned. You've been a super help.

"John Bundy" wrote:

You can do it with anything, but here you go with a toggle switch:

Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
ToggleButton1.Caption = "Uncheck All"
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
ctrl.Value = True
End If
Next ctrl

End If
If ToggleButton1.Value = False Then
ToggleButton1.Caption = "Check All"
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
ctrl.Value = False
End If
Next ctrl

End If

End Sub
--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Freddy" wrote:

You were not repeating. Do you have sample code to make true/make false all
checkboxes using one button [Option/Toggle/Command]? Separately the code was
fine using 2 different Option buttons.

"John Bundy" wrote:

The reply box came up again so I hope I'm not repeating.
This notifies user if all boxes are unchecked:

Private Sub CommandButton1_Click()
Dim ctrl As Control
Dim allOff As Boolean

allOff = True

For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl.Value = True Then allOff = False
End If
Next ctrl
If allOff = True Then MsgBox "All boxes Unchecked"
End Sub

This will check all boxes, change true to false to uncheck them:
Private Sub CommandButton1_Click()
Dim ctrl As Control

For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
ctrl.Value = True
End If
Next ctrl
End Sub

--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Freddy" wrote:

I found that it did so automatically even before your response. Nevertheless,
your input was very helpful. Additionally, do you have sample code for the
following:

To evaluate each checkbox then, if all values are False, notify the user (I
know I can do this part via a msgbox, but not the evaluation part)?
Enable/make True all checkboxes at once?
Disable/make False all checkboxes at once?

"John Bundy" wrote:

It does so Automatically the way you have it written. Might be something to
do with how you built it. I put 3 checkboxes on a userform and a button. the
code for that button follows:

Private Sub CommandButton1_Click()
If CheckBox1.Value = True Then Call customer1
If CheckBox2.Value = True Then Call customer2
If CheckBox3.Value = True Then Call customer3
End Sub

and my subs are as follows, each in a seperate module (my preference)
Sub customer1()
Cells(1, 1) = "Customer1"

End Sub

Sub customer1()
Cells(1, 1) = "Customer1"

End Sub

Sub customer3()
Cells(3, 1) = "Customer3"

End Sub

btw I am using Excel 2003, i'm not an expert on what each are able to do but
this should be no problem.

--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Freddy" wrote:

I wrote sample code as per your example, however, after Sub Customer1() is
done how do I direct the code back to check the value of the next checkbox
and so on? Here is an outline of what I've done so far:

Private Sub UserForm_Initialize()

CheckBox1.Caption = "Customer1"
CheckBox2.Caption = "Customer2"
CheckBox3.Caption = "Customer3"

End Sub

Private Sub CommandButton1_Click()

If CheckBox1.Value = True Then Call SubCustomer1
If CheckBox2.Value = True Then Call SubCustomer2
If CheckBox3.Value = True Then Call SubCustomer3

End Sub

Sub Customer1()
My code€¦
End Sub

Sub Customer2()
My code€¦
End Sub

Sub Customer3()
My code€¦
End Sub


"John Bundy" wrote:

There are various ways, the easiest is probably to insert a new module and
place your code there, such as
Sub Customer1()
MsgBox "Customer Selected"
End Sub

then in the code on the sheet you can call that sub
Call Customer1

then messagebox will appear.
Now just check to see if each is selected and call the appropriate sub routine
--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Freddy" wrote:

I'd like to create a userform containing multiple checkboxes that, after the
user enables the ones selected, will run a corresponding VB macro. For
example, I'd like to create a checklist of customer names, the user selects
the ones to be processed, then the user would click a "Go" button, then all
of the selected customer names would invoke a corresponding but different VB
macro. (Customer1 would run macro1, Customer2 would run macro2... not macro1,
etc.) Does anyone have sample code I can refer to?



All times are GMT +1. The time now is 08:55 PM.

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