Consulting

Results 1 to 12 of 12

Thread: Creating Checkboxes that when click uncheck the other

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,970
    Quote Originally Posted by Leith Ross View Post
    When you need exclusive selection, one out of many, the control of choice is a Radio Button. Trying to alter the inherent functionality of a control when you don't need to is not a good use of your time, unless you just want to know how it is done.
    Yes. OptionButtons (RadioButtons) (I'm talking the Form type, not the ActiveX type) will work as a group if enclosed in a GroupBox. Enclosing the OptionButtons in a GroupBox can be fraught as it seems that the GroupBox should completely enclose the OptionButtons - but I've found that still isn't always the case! Anyway here's a bit of code to add some OptionButtons, and a few (invisible) enclosing GroupBoxes on the active sheet:
    Sub blah()
    With ActiveSheet
      Set rngChkBoxes = .Range("I5,I7,I9,I18")    'only the first cell of each pair
      
      'Delete ALL existing Form groupboxes and Form optionbuttons on the sheet:
      For Each c In .OptionButtons
        c.Delete
      Next
      For Each c In .GroupBoxes
        c.Delete
      Next
        
      For Each cll In rngChkBoxes.Cells
        Set cll2 = cll.Offset(, 1)    'the second optionbutton location
        With .OptionButtons.Add(cll.Left, cll.Top, cll.Width, cll.Height)
          .Caption = ""
          .Height = cll.Height    'needed because it doesn't take on the correct height
        End With
        With .OptionButtons.Add(cll2.Left, cll2.Top, cll2.Width, cll2.Height)
          .Caption = ""
          .Height = cll2.Height    'needed because it doesn't take on the correct height
          '.LinkedCell = cll.Offset(, -1).Address(external:=True) 'this will also be the linked cell for all the option buttons in the same group.
        End With
        Set myRng = .Range(cll, cll2)
        With .GroupBoxes.Add(myRng.Left, myRng.Top, myRng.Width, myRng.Height)
          '.Caption = ""
          .Height = myRng.Height    'needed because it doesn't take on the correct height
          .Visible = False
        End With
      Next cll
    End With
    End Sub
    Last edited by p45cal; 08-16-2019 at 04:30 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Tags for this Thread

Posting Permissions

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