Object problem

  • Thread starter Thread starter Joao
  • Start date Start date
J

Joao

I have 12 optionbuttons and I am trying to run this code:
objOB1...objOB12

Private objOB As Object
....

Set objOB = OptionButton

For n = 1 To 12
If objOB(n).Value = True Then Month = n
Next n

but it gives me a Run-time error '91'
What I am doing wrong?
 
If you have named your OptionButtons objOB1, objOB2, etc then you can use

For n = 1 To 12
If objOB & n = True Then month = n
Next
 
Jesus! Sometimes I'm really so dumb... Thank you very much JLGWhiz, sometimes
we'd get ourselves in such intricated problem, yet the solution is simple
easy...
 
Sorry, at a first look I thought it could work out but then again nothing. It
doesn't work, JLGWhiz.
 
What kind of optionbuttons are they?

Are they on a worksheet from the Forms toolbar?
Are they on a worksheet from the Control toolbox toolbar?
Are they on a Userform (designed inside the VBE)?

If they're on a worksheet from the Forms toolbar:

Dim OptBtn as optionbutton
dim n as long
Dim myMonth as long
mymonth = 0
for n = 1 to 12
if worksheets("sheet9999").optionbuttons(n) = true then
mymonth = n
exit for
end if
next n
if mymonth = 0 then
msgbox "no optionbutton selected"
else
msgbox mymonth
end if

==========
if they're on a worksheet from the control toolbox toolbar and you named them
nicely (Optionbutton1 through Optionbutton12), you could use:

dim OLEObj as OLEObject
dim n as long
Dim myMonth as long
mymonth = 0
for n = 1 to 12
if worksheets("sheet9999").oleobjects("Optionbutton" & n).object.value _
= true then
mymonth = n
exit for
end if
next n
if mymonth = 0 then
msgbox "no optionbutton selected"
else
msgbox mymonth
end if

==========
if they're on a userform you named them nicely (Optionbutton1 through
Optionbutton12), you could use:

Dim Ctrl As Control
Dim n As Long
Dim myMonth As Long
myMonth = 0
For n = 1 To 12
If Me.Controls("Optionbutton" & n).Value = True Then
myMonth = n
Exit For
End If
Next n
If myMonth = 0 Then
MsgBox "no optionbutton selected"
Else
MsgBox myMonth
End If
 
Thank you very much Dave! I just needed one line of code of your help.

Private OLEObj As OLEObject 'OLE para OptionButton (Botões de Opção)

Function DescMeses(intIndice As Byte)
DescMeses = Choose(intIndice, "Janeiro", "Fevereiro", "Março", "Abril",
"Maio", "Junho", _
"Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro")
End Function

Private Sub BotaoOpcoes()
byteMes = Month(Date)

For k = 1 To 12
If OLEObjects("objOB" & k).Object.Value = True Then Mes = k
Next k

DescMes = DescMeses(Mes)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top