Solved

VBA for Excel to add radio buttons with unique group name

Posted on 2007-04-05
8
2,993 Views
Last Modified: 2013-12-25
I am trying to develop code to add radio buttons to the active row, link to a cell in the active row and create a unique group name for this row.  I have code as follows but I understand that the form type of control does not support group name.  

Private Sub ToggleButton1_Click()

Dim Nrcb As Variant
Dim i As Integer
Dim c As Range
Dim Name As String

Nrcb = InputBox("how many checkboxes to place?")
Name = InputBox("Group name?")
ActiveCell.Select

  For i = 1 To Nrcb
   Set c = ActiveCell
       
    ActiveSheet.OptionButtons.Add(c.Left, c.Top, 72, 12).Select
   
    With Selection
        .Characters.Text = ""
        .Value = xlOff
        .LinkedCell = "L" & c.Row
        .Display3DShading = False
        .GroupName = "Name"
       ActiveCell.Offset(0, 2).Activate
    End With
Next i

End Sub

How can I work around this?
0
Comment
Question by:acdecal
  • 5
  • 3
8 Comments
 

Author Comment

by:acdecal
ID: 18858759
I realize that "Name" should not have quotation marks but doesn't work either way.
0
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 18858948
acdecal,

You can always try recording a macro while adding the option buttons from the control toolbox, though it wont record adding a groupname/etc to it. Give this a try:

Private Sub ToggleButton1_Click()
Dim Nrcb As Variant
Dim i As Integer
Dim c As Range
Dim Name As String

Nrcb = InputBox("how many option buttons to place?")
Name = InputBox("Group name?")
Set c = ActiveCell
  For i = 1 To Nrcb
       
    With ActiveSheet.OLEObjects.Add("Forms.OptionButton.1", Left:=c.Left, _
      Top:=c.Top, Width:=72, Height:=12)
     .Object.Caption = ""
     .Object.Value = False
     .Object.GroupName = Name
    End With
   
    Set c = c.Offset(0, 2)
  Next i

End Sub

Matt
0
 

Author Comment

by:acdecal
ID: 18860698
Thanks, but how do I Link a cell to the group?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 35

Expert Comment

by:mvidas
ID: 18860822
If you had 3 option buttons, would you want it to say 1,2,3? A linked cell only says TRUE/FALSE for one option button. You can add a .LinkedCell property in the with block, but unless you're going to have Nrcb linked cells for Nrcb option buttons, a linked cell wouldn't do you much good (especially if they were all linked to the same cell!)

What you could do is have code for each option button that looked something like:

Private Sub OptionButton1_Click()
 Range("L" & OptionButton1.TopLeftCell.Row).Value = 1
End Sub
Private Sub OptionButton2_Click()
 Range("L" & OptionButton2.TopLeftCell.Row).Value = 2
End Sub
Private Sub OptionButton3_Click()
 Range("L" & OptionButton3.TopLeftCell.Row).Value = 3
End Sub

That puts the number into the L cell of the same row. If this sounds good for you, I can see if I can come up with something tomorrow that doesnt require a separate _Click event for each option button added. I know it is doable, but I have no more free time today, and likely won't tonight.  But if you'd like I can do something tomorrow for you

Matt
0
 

Author Comment

by:acdecal
ID: 18860919
Thanks Matt,
Let me explain in more detail what I am trying to accomplish.  The option buttons are part of a code that copies other cells which link to new worksheets as they are created.  The main sheet becomes a summary for the workbook and the user can then pick from seven linked vales for each worksheet (row).  The chosen option button will link the corresponding value to a summary column for each data row.  So I may end up with 20 groups of 7 option buttons.  This workbook will be created for many different jobs.

Hope this makes sense.  Thanks,

Andy
0
 

Author Comment

by:acdecal
ID: 18860974
More on that:  If a cell was linked to each group then a conditional function could chose the corresponding value and link it to a summation column.
0
 

Author Comment

by:acdecal
ID: 18864122
Hey Matt,

I think I figured it out:


Private Sub ToggleButton1_Click()

Dim Nrcb As Variant
Dim i As Integer
Dim c As Range
Dim Name As String
Dim cb As OLEObject

Nrcb = InputBox("how many checkboxes to place?")
Name = InputBox("Group name?")
ActiveCell.Select

  For i = 1 To Nrcb
   Set c = ActiveCell
 
Set cb = ActiveSheet.OLEObjects.Add("Forms.OptionButton.1", Left:=c.Left, _
     Top:=c.Top, Width:=72, Height:=12)
  cb.LinkedCell = c.Offset(1, 0).Address
  cb.Object.Caption = ""
  cb.Object.Value = False
  cb.Object.GroupName = Name
  ActiveCell.Offset(0, 2).Activate
 
  Next i

End Sub

Each option button is linked to the cell below and
I then can use the =SUMIF(TRUE) function to capture the value selected for each set.

Thanks for your help. I'll give you the points.


0
 
LVL 35

Expert Comment

by:mvidas
ID: 18864381
I've actually been playing around with this the past couple hours, and I have to say that VBA doesnt like to play by the rules sometimes. I have a class module to control the events for the option buttons, but after the subroutine runs that adds them the collection storing them seems to forget itself.  But running two subs separately (to add buttons, then to get them working) works fine! At least we can get the option buttons working on workbook_open so your end users won't know the difference.

Anyways, I know you are happy with your method, but out of my own personal stubbornness I want to get to the bottom of this.  If you'd like, take a look at where I'm at right now: https://filedb.experts-exchange.com/incoming/ee-stuff/3113-hmmm.zip

Use the "CreateOptionButtons" macro to create buttons the same way your sub above works.
Use the "GetOptionButtonsWorking" macro to get them working
Use the "DeleteAllOptionButtons" to delete them from the active sheet

On workbook open it will get any existing optionbuttons working, but you'll still have to run "getoptionbuttonsworking" after adding your buttons. The # of the option button for each row will be put into column L (easily changeable). I know you're happy with what you have, as I said I guess I'm just stubborn sometimes :)

Matt
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now