PDA

View Full Version : Solved: Option Buttons



maninjapan
09-18-2008, 03:13 AM
I am succesfully using the following, very basic stuff.

If OptionBuyJan Then Cells(NextRow, 2) = "January"

But is it possibly to use a combination of OptionButtons to create data.

For example

If OptionBuyDec And OptionBuy2008 Then Cells(NextRow, 3) = "Dec 08"

I have tried this as is but it didnt return anything.

Bob Phillips
09-18-2008, 03:18 AM
You can't have two option buttons set at the same time.

CreganTur
09-18-2008, 10:22 AM
You can't have two option buttons set at the same time.

Actually you can have multiple option buttons selected at the same time, but all of the different groups of option buttons you want to use have to be encased by their own frames.

Using the Month and Year example from the first post, you would need to have a frame for Months that has option buttons for every month. Then you could have another frame called Years with option buttons for the different years.

You cannot select multiple option buttons in the same frame.

NinjaEdithttp://img293.imageshack.us/img293/9060/ninja3od8.gif: I added an example spreadsheet that shows how to use multiple option buttons. When it opens to Sheet1, press Ctrl + Q to launch the UserForm where you can make multiple option button selections and see a result.

Bob Phillips
09-18-2008, 10:39 AM
You cannot select multiple option buttons in the same frame.

CreganTur
09-18-2008, 11:18 AM
That's strange... How did you set the value of both option buttons to true? The spreadsheet I uploaded (and other spreadsheets where I use them) won't let me select 2 options buttons in the same frame.

Bob Phillips
09-18-2008, 11:43 AM
I was messing with you!

Option buttons have a groupname property that you can set. If they are in different groups, you can set them both, regardless of whether they are in the same frame, different frames, or no frames.

CreganTur
09-18-2008, 12:16 PM
I was messing with you!

Curse you, El Cid! :neener:


Option buttons have a groupname property

I learn something new every day :read2:

maninjapan
09-18-2008, 10:51 PM
XLD so does that invalidate your earlier comment about not being able to set 2 optionbuttons at the same time?? CreganTur, Thanks I see how that works.

maninjapan
09-18-2008, 11:50 PM
c

maninjapan
09-18-2008, 11:51 PM
CreganTur, What does the 'Me. ' part of this statement signify. Would it work without it?

If Me.optJan = True Then
strMonths = "January"

Bob Phillips
09-19-2008, 02:06 AM
XLD so does that invalidate your earlier comment about not being able to set 2 optionbuttons at the same time?? CreganTur, Thanks I see how that works.

That comment was made as a response to your question, as I interpreted. If you WANT to set two buttons at a time, you have to program it accordingly.

maninjapan
09-19-2008, 02:46 AM
Ok. and thanks for your help, Ive been able to do what I wanted to do now.

CreganTur
09-19-2008, 05:22 AM
CreganTur, What does the 'Me. ' part of this statement signify. Would it work without it?

If Me.optJan = True Then
strMonths = "January"

'Me' is a special keyword that refers to the form that the code is behind. It's a very simple way to refer to objects on the UserForm- it's a lot cleaner and simpler than the longhand method.

RonMcK
09-19-2008, 10:01 AM
NinjaEdithttp://img293.imageshack.us/img293/9060/ninja3od8.gif: I added an example spreadsheet that shows how to use multiple option buttons. When it opens to Sheet1, press Ctrl + Q to launch the UserForm where you can make multiple option button selections and see a result.

Randy,

Your example runs afoul as Excel helps you by interpreting the date and formating it in the cell. I'm attaching a jpeg with the output from executing your code and my added code lines, below. (code is from your UserForm)

Private Sub btnExecute_Click()

Dim strMonths As String
Dim strYears As String
Const strDay As String = "01" ' added constant

If Me.optJan = True Then
strMonths = "January"
ElseIf Me.optDec = True Then
strMonths = "December"
End If

If Me.opt07 = True Then
strYears = "07"
ElseIf Me.opt08 = True Then
strYears = "98" ' for A4 entry; was '08' originally
End If

Sheet1.Range("A4").Value = strMonths & " " & strYears ' where strYears > 31
' Sheet1.Range("A3").Value = strMonths & " " & "20" & strYears
' Sheet1.Range("A2").Value = strMonths & " " & strDay & " " & strYears
' Sheet1.Range("A1").Value = strMonths & " " & strYears ' XL interprets strYears as being a day date

End Sub


Cheers,