Sunday, 10 December 2017

windows - How to select radio buttons by cell value in Excel?


I have a group of radio buttons with labels in adjacent cells. How can I arrange for the radio button with the label matching a target cell value to activate?


Here's an equivalent example to what I'm trying to achieve:


enter image description here


Here, you can see two radio groups: "buyer" and "destination". The labels are in column A, the radio groups are in column B, and the target values are in column C. The values in column C are what we control, and we want to activate the correct radio button by looking up the value of the target cell in the list of labels in column A. That is, if C2 changed to "Internal", then we want the radio button in cell B4 to activate. The radio buttons in B7 and B8 will be unaffected as they are in a different group.



Answer



You don't need to actually read the label value. You can assign the labels to option buttons and change the value of the option selected that way.


You could call this on the Worksheet_SelectionChange


Sub reflectCell()
Dim targetedCell As String
targetedCell = Range("A1").Text

Select Case UCase(targetedCell)
Case "RED"
ActiveSheet.OptionButton1.Value = True
Case "BLUE"
ActiveSheet.OptionButton2.Value = True
Case "GREEN"
ActiveSheet.OptionButton3.Value = True
End Select

End Sub


Unless you mean the label IS a cell value adjacent to the OptionButton Is the target cell a static Range location or the current ActiveCell?


If the target cell is always the same location you might control the input spelling like this. enter image description here


Here is another simpler approach


A non-macro solution that works if you can use formulas on the sheet is shown in this screen capture


option buttons example


Name each option button the same as the Label in column A Have it's linked cell be another cell (which can be hidden examples in E are the formulas showing in F) in that cell have it compare the input you provide to each cell label. Only one should end up being TRUE, (see example formulas)


Thus the option in B2 is linked to E2, B3 is linked to E3, B7 is linked to E7 etc. If you are allowed the extra column with formulas( and they can be anywhere) this is much simpler than using vba


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...