Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    May 2004
    Location
    Toronto, Ontario, Canada
    Posts
    425
    Thanks
    5
    Thanked 0 Times in 0 Posts

    For each next control (Excel 2003)

    Hi all,
    I am trying to test a set of optionbuttons on a form within a frame. What I would like to accomplish is to test to see if all of the the option buttons are false within Frame1. What happens now is it doesn't recognize the true value even if one of them has been selected.

    Thanks,
    Darryl.

    dim x as Control
    For Each x In Frame1.Controls
    If x.Value = True Then
    ActiveCell.Value = x.Caption
    End If
    Next
    For Each x In Frame1.Controls
    If x.Value = False Then
    MsgBox "Select press to continue ", vbCritical
    Exit Sub
    End If
    Next

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: For each next control (Excel 2003)

    That code looks fine to me and works OK in Excel 2002 - do you not get anything put in the active cell? (I assume your option buttons do have captions?)
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    4 Star Lounger
    Join Date
    May 2004
    Location
    Toronto, Ontario, Canada
    Posts
    425
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re: For each next control (Excel 2003)

    hi Rory,
    thanks for your reply, it does work, the information does go into the cell, but what it doesn't do is if the value is true It still sees the group as false, because of I still get the msgbox. I guess what I should ask is, is the For next loop the right way to test a group of optionbuttons when testing to see if all of them are false?

    Thanks
    Darryl.

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: For each next control (Excel 2003)

    You still get the message box because you are testing each control in the frame to see if they are False and if one of them is, the message box is triggered.
    If you just want to determine if none of them are true you could use something like:
    Function AreAnyTrue() as Boolean
    Dim ctl as Control
    AreAnyTrue = False
    For each ctl in Frame1.controls
    if ctl.value = true then
    AreAnyTrue = True
    Exit Function
    End If
    next ctl
    End Function
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    4 Star Lounger
    Join Date
    May 2004
    Location
    Toronto, Ontario, Canada
    Posts
    425
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re: For each next control (Excel 2003)

    Thanks Rory

Posting Permissions

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