+ Reply to Thread
Results 1 to 3 of 3

Iterate through a form

  1. #1
    Registered User
    Join Date
    09-16-2005
    Posts
    7

    Iterate through a form

    Hi. This is probably easy stuff, but I can't seem to do it!

    Here goes...

    I have a form that contains several OptionButtons. These are all held in a frame on my UserForm. There are also Labels in the frame next to the OptionButton that identifies its purpose to the user.

    I would like to find a way of iterating through the frame to identify which OptionButton has been selected. Then, using the associated Caption of the OptionButton I would like to enter this value into a cell on a worksheet.

    I've been having real trouble with this. I'm just trying to do a project for work that will really make my life easier there, I'm not an Excel superuser!

    If anyone can help I'd be real grateful.

    Thanks,

    Rob.

  2. #2
    Dave Peterson
    Guest

    Re: Iterate through a form

    Is there a reason you didn't use the caption of the optionbutton as the
    identifier?

    If I named the optionbuttons and labels nicely, this worked ok:

    Option Explicit
    Private Sub CommandButton1_Click()
    Unload Me
    End Sub
    Private Sub CommandButton2_Click()
    Dim myCtrl As Control
    Dim iCtr As Long
    Dim FoundIt As Boolean
    iCtr = 0
    FoundIt = False
    For Each myCtrl In Me.Frame1.Controls
    If TypeOf myCtrl Is MSForms.OptionButton Then
    iCtr = iCtr + 1
    If myCtrl.Value = True Then
    MsgBox myCtrl.Caption
    FoundIt = True
    Exit For
    End If
    End If
    Next myCtrl

    If FoundIt = True Then
    MsgBox Me.Frame1.Controls("Label" & iCtr).Caption
    Else
    MsgBox "Nothing selected"
    End If
    End Sub

    That "msgbox myctrl.caption" might be all you really need with a minor change to
    the form???

    RobEdgeler wrote:
    >
    > Hi. This is probably easy stuff, but I can't seem to do it!
    >
    > Here goes...
    >
    > I have a form that contains several OptionButtons. These are all held
    > in a frame on my UserForm. There are also Labels in the frame next to
    > the OptionButton that identifies its purpose to the user.
    >
    > I would like to find a way of iterating through the frame to identify
    > which OptionButton has been selected. Then, using the associated
    > Caption of the OptionButton I would like to enter this value into a
    > cell on a worksheet.
    >
    > I've been having real trouble with this. I'm just trying to do a
    > project for work that will really make my life easier there, I'm not an
    > Excel superuser!
    >
    > If anyone can help I'd be real grateful.
    >
    > Thanks,
    >
    > Rob.
    >
    > --
    > RobEdgeler
    > ------------------------------------------------------------------------
    > RobEdgeler's Profile: http://www.excelforum.com/member.php...o&userid=27336
    > View this thread: http://www.excelforum.com/showthread...hreadid=468356


    --

    Dave Peterson

  3. #3
    Registered User
    Join Date
    09-16-2005
    Posts
    7

    Smile Thanks!

    Thank you very much for your help, I'm pleased to say that it works really good!

    Have a nice day.

    Rob.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1