Select option if value is between certain range

joacro

Board Regular
Joined
Jun 24, 2010
Messages
158
Hi there all,

I have a userform with a txtbox and 3 option boxes.

If the value is between a certain value one of the three option boxes need to be automatically selected

If txtbox1 <=2 then opt1 must be selected
if txtbox1 >2 but smaller than 18 then opt2 must be selected
if txbox1>=18 then opt3 must be selected

Hope it makes sense and someone will be able to help me with some code

Regards
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Maybe "option boxes" (I assume you mean option buttons) is not the way to go. Perhaps I'm being picky but if something "must be selected" then it really isn't an option. Exactly what are you trying to do? Are these option buttons "grouped" so that their selection is mutually exclusive?

Gary
 
Upvote 0
Hi Gary,

Yes my option buttons is grouped and the option botton then has other code linked to it

Basically it is for the age thing that I requested previously,

I need the option to be selected automatically when the value is between a spesific range

Can it be done
 
Upvote 0
I'm certain it can be done but how do you propose to prevent the user from selecting option button 2 or 3 after the code decides that it should be option 1 that gets selected?

It may be possible to select option 1 (using my above example) and then disable the whole group so the user can't change the auto selection made by the code but then you will need a way to re-enable the option buttons. This scheme seems to have the ability to quickly turn into a can of worms.

This is why I'm asking exactly what you are trying to do. Maybe you should be just informing the user, with a label perhaps, that "The data you entered in the text box will cause this or that to happen ... are you sure?"

Gary
 
Upvote 0
Hi Gary,

Well the option boxes are linked to the txtCh1 and are enabled when the txtCh1 has a value.

Would it be posible to grey it out the two option that is not automatically selected because of the value in txtage

And on the other hand if no dob is entered then the user can choose which age group to choose

So basically I have

txtCh1 when a value is entered opt1, opt2, opt3 is enabled
txtDob and txtAge which you helped me to calculate the age

1. I would like the user to be able to choose opt 1, or opt2 or opt3 if no date of birth is entered

2 And if a date of birth is entered I would like opt1, or opt2 or opt3 to be automatically selected and the other two options greyed out.

Hope you will be able to help me
 
Upvote 0
I believe something like the following is what you originally asked for. I've never seen a Windows application work like this (forcing option buttons to change and then disabling them). I would suggest something like 3 different forms or a multiple tabbed form wherein the correct form or tab is displayed depending on what you enter in the text box.

In any case, I hope this helps.

Gary

Code:
Private Sub txtBox1_AfterUpdate()

'Expects the following:

'Text Box named: txtBox1
'Framed Option button named: Opt1
'Framed Option button named: Opt2
'Framed Option button named: Opt3
'Frame named: Frame1

'Rename controls & change code to match if desired

Select Case Val(txtBox1.Text)

    Case Is <= 2
        Opt1.Value = True
        Opt2.Enabled = False
        Opt3.Enabled = False

    Case Is >= 18
        Opt3.Value = True
        Opt1.Enabled = False
        Opt2.Enabled = False

    Case Is > 2
        Opt2.Value = True
        Opt1.Enabled = False
        Opt3.Enabled = False

End Select

If txtBox1.Text = "" Then
    Opt1.Enabled = True
    Opt2.Enabled = True
    Opt3.Enabled = True
End If

'Optionally, disable frame instead of individual buttons and remove OptX.Enabled = False (above)
'Frame1.Enabled = False

End Sub
 
Upvote 0
Did you see my notes in the code regarding the names of the controls?

Did you paste the code in the "AfterUpdate" event of the correct textbox?

Gary
 
Upvote 0
Hi Gary,

Yes I think I did, here is what I did

Private Sub txtAge_AfterUpdate()

'Expects the following:
'Text Box named: txtAge
'Framed Option button named: Opt1
'Framed Option button named: Opt2
'Framed Option button named: Opt3
'Frame named: Frame1
'Rename controls & change code to match if desired
Select Case Val(txtAge.Text)
Case Is <= 2
opt1.Enabled = True
opt2.Enabled = False
opt3.Enabled = False
Case Is >= 18
opt3.Enabled = True
opt1.Enabled = False
opt2.Enabled = False
Case Is > 2
opt2.Enabled = True
opt1.Enabled = False
opt3.Enabled = False
End Select
If txtAge = "" Then
opt1.Enabled = True
opt2.Enabled = True
opt3.Enabled = True
End If
'Optionally, disable frame instead of individual buttons and remove OptX.Enabled = False (above)
'Frame1.Enabled = False

End Sub

So this means that if txtAge is updated the options will be selected Correct?
 
Upvote 0

Forum statistics

Threads
1,216,070
Messages
6,128,610
Members
449,460
Latest member
jgharbawi

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