ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Object problem (https://www.excelbanter.com/excel-programming/406619-object-problem.html)

Joao

Object problem
 
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?

JLGWhiz

Object problem
 
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


"Joao" wrote:

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?


Joao

Object problem
 
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...

"JLGWhiz" wrote:

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


"Joao" wrote:

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?


Joao

Object problem
 
Sorry, at a first look I thought it could work out but then again nothing. It
doesn't work, JLGWhiz.

"Joao" wrote:

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...

"JLGWhiz" wrote:

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


"Joao" wrote:

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?


Dave Peterson

Object problem
 
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



Joao wrote:

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?


--

Dave Peterson

Joao

Object problem
 
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





"Dave Peterson" wrote:

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



Joao wrote:

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?


--

Dave Peterson



All times are GMT +1. The time now is 12:29 PM.

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