TRUE/FALSE MACRO using Option Buttons

J

Jase

I have and IF statement with multiple TRUE/FALSE parameters. I am trying to
convert it onto a userform using option buttons. for example on my excel
sheet i have A1-A5 all false/true (with only 1 being true). How do I convert
this over to my userform using multiple option option buttons; each option
button linked to a cell A1-A5? If this is not clear please let me know.

basically if A1 is true, A2-A5 are false
if A2 is true A1 and A3-A5 are false, I need a macro on how to use this on a
userform
 
K

Kevin B

Place a frame object on your user form, and then insert 5 option buttons
within the frame.

In the form's code module you can use the following code as long as the
option button have their default names and the worksheet you want to update
is sheet 1, if not modify the code as needed:

'The following private function has one
'argument, the buttons number

Private Sub OnOff(intButton As Integer)

Dim ws As Worksheet
Dim rng As Range
Dim i As Integer
Dim blnOnOff(4) As Boolean

Set ws = ThisWorkbook.Sheets(1)
Set rng = ws.Range("A1")

Select Case intButton
Case 1
blnOnOff(0) = True
blnOnOff(1) = False
blnOnOff(2) = False
blnOnOff(3) = False
blnOnOff(4) = False
Case 2
blnOnOff(0) = False
blnOnOff(1) = True
blnOnOff(2) = False
blnOnOff(3) = False
blnOnOff(4) = False
Case 3
blnOnOff(0) = False
blnOnOff(1) = False
blnOnOff(2) = True
blnOnOff(3) = False
blnOnOff(4) = False
Case 4
blnOnOff(0) = False
blnOnOff(1) = False
blnOnOff(2) = False
blnOnOff(3) = True
blnOnOff(4) = False
Case 5
blnOnOff(0) = False
blnOnOff(1) = False
blnOnOff(2) = False
blnOnOff(3) = False
blnOnOff(4) = True
End Select

For i = 0 To 4
rng.Offset(i).Value = blnOnOff(i)
Next i

Set ws = Nothing
Set rng = Nothing
End Sub

'To each option button assign the sub using the
'option button's number as the argument


Private Sub OptionButton1_Change()

OnOff 1

End Sub

Private Sub OptionButton2_Change()

OnOff 2

End Sub

Private Sub OptionButton3_Change()

OnOff 3
End Sub

Private Sub OptionButton4_Change()

OnOff 4

End Sub

Private Sub OptionButton5_Change()

OnOff 5

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top