Why am I getting a type mismatch?

D

darius

Hi

Extreme newbie here. I don't know why I'm getting a type mismatch error
on this function

Private Function opt_to_text(acol As Integer) As String ' <-- error here
Dim optCheck As MSForms.OptionButton
For x = acol To acol + 3
Set optCheck = "OptionButton" & CStr(x)
If optCheck.Value = True Then
opt_to_text = optCheck.Caption
End If
Next x
End Function

msgbox(opt_to_text(1))

thanks
 
T

Tom Ogilvy

If on a userform:

Private Function opt_to_text(acol As Integer) As String ' <-- error here
Dim optCheck As MSForms.OptionButton
For x = acol To acol + 3
Set optCheck = Controls("OptionButton" & CStr(x))
If optCheck.Value = True Then
opt_to_text = optCheck.Caption
End If
Next x
End Function

If on a worksheet

Private Function opt_to_text(acol As Integer) As String ' <-- error here
Dim optCheck As MSForms.OptionButton
For x = acol To acol + 3
Set optCheck = Activesheet.OleObjects("OptionButton" &
CStr(x)).Object
If optCheck.Value = True Then
opt_to_text = optCheck.Caption
End If
Next x
End Function
 
D

darius

If on a userform:

Private Function opt_to_text(acol As Integer) As String ' <-- error
here
Dim optCheck As MSForms.OptionButton
For x = acol To acol + 3
Set optCheck = Controls("OptionButton" & CStr(x))
If optCheck.Value = True Then
opt_to_text = optCheck.Caption
End If
Next x
End Function

If on a worksheet

Private Function opt_to_text(acol As Integer) As String ' <-- error
here
Dim optCheck As MSForms.OptionButton
For x = acol To acol + 3


this is it!!
Set optCheck = Activesheet.OleObjects("OptionButton" &
CStr(x)).Object

thanks :)
 

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

Top