+ Reply to Thread
Results 1 to 13 of 13

Make Check Boxes behave as Option buttons

Hybrid View

nikolab Make Check Boxes behave as... 10-27-2009, 03:01 PM
Leith Ross Re: Make Check Boxes behave... 10-27-2009, 03:55 PM
Palmetto Re: Make Check Boxes behave... 10-27-2009, 04:08 PM
nikolab Re: Make Check Boxes behave... 10-27-2009, 04:24 PM
Palmetto Re: Make Check Boxes behave... 10-27-2009, 04:43 PM
rwgrietveld Re: Make Check Boxes behave... 10-27-2009, 06:02 PM
mikerickson Re: Make Check Boxes behave... 10-27-2009, 09:25 PM
rwgrietveld Re: Make Check Boxes behave... 10-28-2009, 03:01 AM
mikerickson Re: Make Check Boxes behave... 10-28-2009, 11:15 AM
mikerickson Re: Make Check Boxes behave... 10-29-2009, 06:48 PM
  1. #1
    Registered User
    Join Date
    10-27-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Make Check Boxes behave as Option buttons

    Hi,

    I have a form with at about 1200 checkboxes and I need to make them behave as radio buttons. What I mean is that just one box can be selected in a group of boxes.

    Lets say i want to group all 1200 boxes in 400 groups, with 3 check boxes in every group. I want user to be able to select just one check box in every group.

    Could this happen somehow?

    Thank you in advance,
    Nikola

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Make Check Boxes behave as Option buttons - just one can be selcted

    Hello nikolab,

    Welcome to the Forum!

    I have to ask why you don't want to use option buttons. Option buttons are designed to be mutually exclusive - only one sets at a time. What type of form are you referring to: a worksheet or VBA user form?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Make Check Boxes behave as Option buttons - just one can be selcted

    If your checkboxes are not Active-X type, then I don't see how you can get this to work cleanly without a lot of messy code. I would delete them and use Option buttons.

    If your check boxes are from the Active-X type controls then you could use the click event with code similar to this:
    Option Explicit
    
    Private Sub CheckBox1_Click()
        With Me
            If .CheckBox1.Value = True Then
                .CheckBox2.Value = False
                .CheckBox3.Value = False
            Else
                Exit Sub
            End If
        End With
    End Sub
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  4. #4
    Registered User
    Join Date
    10-27-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Make Check Boxes behave as Option buttons - just one can be selcted

    Hi,

    I have the forms ready. I have 6 forms with at about 1200 checkboxes each. That is why I don't want to replace them with option buttons. And also I have a lot of calculations in the forms, so replacing the checkboxes with option buttons will take me something like a month. I have everything on a worksheet and my controls are not ActiveX type.

    BR,
    Nikola

  5. #5
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Make Check Boxes behave as Option buttons - just one can be selcted

    Here is a formula approach that might work for you, but it will take A LOT of manual work to get it set up.

    Assume three check boxs linked to cells A1:A3
    Using cells B1:B3 to contain a formula to test each other, you might use this approach:

    B1: =IF(AND(A1=TRUE,OR(A2=TRUE,A3=TRUE)),"Make one Selection ONLY",A1)
    B2: =IF(AND(A2=TRUE,OR(A1=TRUE,A3=TRUE)),"Make one Selection ONLY",A2)
    B3: =IF(AND(A3=TRUE,OR(A1=TRUE,A2=TRUE)),"Make one Selection ONLY",A3)

    It will not stop them from checking more than one box in the absolute sense, but it does return a message and can return a corect value when all is in order.

  6. #6
    Registered User
    Join Date
    10-27-2009
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Make Check Boxes behave as Option buttons - just one can be selcted

    Hi Palmeto,

    Thank you for your idea, but this is not exactly what I need. I just need the option button behaviour and nothing else.

    Nikola

  7. #7
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Make Check Boxes behave as Option buttons - just one can be selcted

    The help you even a bit further:
    Sub add_opt()
    
    Dim OptBut(1 To 9) As Control
    Dim MyMod As Integer, GroupName As Integer
    
    For i = 1 To 9
      MyMod = (i - 1) Mod 3 + 1  '1,2,3,  1,2,3,  1,2,3
      GroupNum = CInt(Application.WorksheetFunction.RoundDown((i - 1) / 3 + 1, 0))  '1,1,1,  2,2,2,  3,3,3
      Debug.Print MyMod, GroupNum
      Set OptBut(i) = UserForm1.Controls.Add("Forms.OptionButton.1", "OptBut" & i, True)
      With OptBut(i)
        .GroupName = GroupNum
        .Width = 20
        .Left = MyMod * (.Width + 2) + 10
        .Top = GroupNum * 15
      End With
    Next i
    
    End Sub
    Last edited by rwgrietveld; 10-27-2009 at 06:11 PM.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Make Check Boxes behave as Option buttons - just one can be selcted

    Given a useform, this will replace all check boxes with option buttons, preserving their postition and captions. Note that the name of the new option button is the same as the checkbox so that the event code does not need to be rewritten.

    This should be run once at design time, it need not be run every time at runtime.

    Sub CheckBoxesToOptionButtons()
        Dim myUF As Object
        Dim oneControl As Object
        Set myUF = ThisWorkbook.VBProject.vbcomponents("Userform1")
        
        With myUF.designer
            For Each oneControl In .Controls
                If TypeName(oneControl) = "CheckBox" Then
                Call OptionForCheck(oneControl, oneControl.Parent)
                End If
            Next oneControl
        End With
    End Sub
    
    Sub OptionForCheck(oldCheckBox As Object, cbParent As Object)
        Dim oName, oHeight, oWidth, oTop, oLeft, oCaption
        With oldCheckBox
            oName = .Name
            oHeight = .Height
            oWidth = .Width
            oTop = .Top
            oLeft = .Left
            oCaption = .Caption
        End With
        If TypeName(cbParent) = "Userform" Then Set cbParent = cbParent.designer
        
        With cbParent.Controls
            .Remove (oName)
            With .Add("Forms.OptionButton.1")
                .Name = oName
                .Caption = oCaption
                .Left = oLeft
                .Top = oTop
                .Height = oHeight
                .Width = oWidth
            End With
        End With
    End Sub
    Last edited by mikerickson; 10-27-2009 at 09:48 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Make Check Boxes behave as Option buttons

    Mike,

    Nice code, looks good but for me this specific code
        Set myUF = ThisWorkbook.VBProject.VBComponents("Userform1")
    does not seem to work. VBProject failed.
    Any idea?

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Make Check Boxes behave as Option buttons

    In the VBEditor Tools>References, is your Microsoft Visual Basic for Applications Extensibility library checked?

    I'm still on my first cup of coffee, but I don't think that the code should crash there if the VBIDE isn't active, it should crash somewhere else.

    Also, are you on a PC? I'm on a Mac and this code is right in the middle of differences between the platforms. It would be good to know if it works on both.

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Make Check Boxes behave as Option buttons

    One quick update, I just tested the code on the Excel 2007 at work and it ran OK.

+ 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