How to count Option button clicks on same userform?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Please can someone help me?

I have a userform called OPTIONS.

I have 6 different options on the userform.

What I would like is if an operator selects an option it counts the number of times that selection has been selected in a textbox on the same userform.

For example:

If OptionButtonADD is selected then an 1 appears in TextBoxADD and if it is selected again then a 2 appears in the relevant textbox and so on.

Once I get the first code for the ADD I will be able to do the rest.

I would like a reset count code as well please to reset all the textboxes.

Any help would much appreciated.

Thanks

Dan
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
There are various events associated with the option button such as Click, Mouse Down etc. So I would suggest using Click to increment a counter for each option button
 
Upvote 0
Try this

Code:
Private Sub OptionButtonAdd_Click()
    With TextBoxADD
        If .Value = "" Then .Value = 0
        .Value = .Value + 1
    End With
End Sub
 
Upvote 0
Hi Yongle,

Unfortunately its not working.

I found a code very similar to yours earlier and that didn't work either and I don't know why?

The only way I've been able to get it to work so far is by using a cell on the worksheet as the counter and then using the control source from the textbox options and have it look at that cell, but it isn't the way I would like it.

Any further ideas would be appreciated.

Thanks

Dan
 
Upvote 0
Try something like this.
Change control names if needed:

Code:
Private Sub OptionButton1_Click()
'Modified  4/16/2019  7:32:25 AM  EDT
If TextBox1.Value = "" Then TextBox1.Value = 1: OptionButton1.Value = False: Exit Sub
TextBox1.Value = TextBox1.Value + 1
OptionButton1.Value = False
End Sub
 
Upvote 0
Unfortunately its not working.
I found a code very similar to yours earlier and that didn't work either and I don't know why?
The only way I've been able to get it to work so far is by using a cell on the worksheet as the counter and then using the control source from the textbox options and have it look at that cell, but it isn't the way I would like it.
Any further ideas would be appreciated.
Thanks

Dan
I tested it before posting and it does work
- so there is something different in your form (or its code)

I assumed you were resetting your option button elsewhere. If you are not doing that
Code:
Private Sub OptionButtonAdd_Click()
    With TextBoxADD
        If .Value = "" Then .Value = 0
        .Value = .Value + 1
    End With
    [COLOR=#ff0000]OptionButtonAdd.Value = False[/COLOR]
End Sub

Is OptionButtonADD an OptionButton or a CommandButton?
(I have it working with both)
 
Last edited:
Upvote 0
Hi,

Right, what I think it is, is when I close the userform to open another it is clearing the count.

I saw it because of My Answer Is This's code. With your code I have to select the ADD option twice but after the first click I saw the counter work.

Here is my code as it is now:

Code:
Private Sub OptionButtonADD_Click()

If TextBoxADD.value = "" Then TextBoxADD.value = 1: OptionButtonADD.value = False: Exit Sub
TextBoxADD.value = TextBoxADD.value + 1
OptionButtonADD.value = False

' With TextBoxADD
'        If .value = "" Then .value = 0
'        .value = .value + 1
'    End With
'    OptionButtonADD.value = False


'Unload Me


Me.Hide

OpenNewProduct

End Sub


I prefer Yongle's code because I only have to select the option once.

Is there any issues with me having the userfom hidden instead of being closed?

Thanks guys I really appreciate your help.



gsbelbin, thank you as well for your advice but I am going to persist with the click event, I think we are nearly there.

Thanks

Dan
 
Upvote 0
If you hide the UserForm the values in the UserForm are maintained.
But those values will be lost when you close the Workbook.
 
Upvote 0
Hi,

There is no way around it apart from linking the userform textbox to a cell on the worksheet?

I will lose the counter qty if I either unload the userform or close the worksheet as it is now?

Thanks

Dan
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,020
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top