View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Mark Burns[_2_] Mark Burns[_2_] is offline
external usenet poster
 
Posts: 1
Default Set OptionBox value via VBA

I just though I'd post the results of my experience trying to do something
which I would have thought would be simple. In the end it was simple, but you
have to know some somewhat obsure things to do it right, apparently.
The task:
An (Userforms/MSForms, NOT an ActiveX) Group control and two enclosed Option
button controls (one for Yes, one for No) are on a spreadsheet. Set the
option button value(s) to Yes or No via VBA code as read from a database
field.
How would you think this would be done?
Set the value of the ONE control which is "Picked"/On to True(=-1 in VBA),
right? Wrong. Set it to the constant value xlON (= 1 in VBA).
Also, it seems to depend upon whether the control is bound to a cell in a
workbook or not. According to my recent experience, if you do this:
ActiveWorksheet.Shapes("OptionButton1").ControlFor mat.Value = xlOn
This will set an option button to ON. Interestingly, if you wish to set that
same option button to OFF you need the xlOFF constant, whose value is NOT 0
(which is what VB programmers might intuitively think it ought to be), it is
-4146. go figure.

However, this may - or may not - work properly if you are setting the value
of a control that IS bound to a worksheet cell (i.e.
ActiveWorksheet.Shapes("OptionButton1").LinkedCell ="B19" ).
In that circumstance, you may be better off in changing the option button
values by changing the cell value directly.
But, what values do you use to set the option buttons in the cell? xlON and
xlOff, right? wrong.
The values you need to set in the cell are 2 or 3 in this example. 2!??
3!??? Where the HECK did those values come from? I haven't a clue. Ths only
way I found out what the values to use was to set up the control, bind them
to a common cell, and click them each, observing what values appeared in the
cell.
Well, at leasst it worked, and hopefully, this should help someone else
puzzle through this forms-control-in-worksheet weirdness too.