Anyone know why this doesn't work ?

K

kirkm

There's a Form with 2 checkboxes and these Click events

Private Sub chkK1_Click()
chkK1 = True
chkA1 = False
Application.StatusBar = "chkK1"
End Sub

Private Sub chkA1_Click()
chkA1 = True
chkK1 = False
Application.StatusBar = "chkA1"
End Sub


The intent is, as chkK1 is ticked, chkA1 will bceome unticked and Vice
versa.

But it doesn't work! How can it NOT work ???

I've also tried .Value = and .Caption = and removed' = True'
(in case that was automatic), but still no go.

What am I doing wrong? At least the Application.StatusBar message
works !

Thanks - Kirk
 
K

kirkm

So what happens? I would expect, by reading this code, that you could
only untick a box by ticking the other one.

That's exactly what I want, i.e you can only choose one of them.
But what does happen is odd. Try it out. I think Excels applying a
built in toggle which won't disable.
 
K

kirkm

Works as expected. I click on box 1 and, regardless of its previous
state, it becomes or remains selected, box 2 becomes or remains
unselected, and status bar reads box 1. Similar for box 2. And, box 1
and box 2 always have opposite state.

Weird. That sounds like what I wanted it to do.... but doesn't.
The first click on Box1 select 1 with Box2 is unselected. Then click
Box2 and nothing happens. (Except the status bar message)
What are you trying to do? If you want mutually exclusive selections,
options buttons are the control of choice.

Yes, that works. But check boxes were preferred.
 
L

Lars-Åke Aspelin

There's a Form with 2 checkboxes and these Click events

Private Sub chkK1_Click()
chkK1 = True
chkA1 = False
Application.StatusBar = "chkK1"
End Sub

Private Sub chkA1_Click()
chkA1 = True
chkK1 = False
Application.StatusBar = "chkA1"
End Sub


The intent is, as chkK1 is ticked, chkA1 will bceome unticked and Vice
versa.

But it doesn't work! How can it NOT work ???

I've also tried .Value = and .Caption = and removed' = True'
(in case that was automatic), but still no go.

What am I doing wrong? At least the Application.StatusBar message
works !

Thanks - Kirk

A problem with your code seems to be that the manipulation of the
checkbox value from inside the code also fires the click event.
You thereby get into an "infinite loop" that is only interrupted
because some internal event buffer (or what it might be called) gets
overloaded.
Put this statement as the first statement of any of you click
procedures and you will see from the behaviour of cell A1 what I mean

Worksheets("Sheet1").Cells(1, 1) = Worksheets("Sheet1").Cells(1, 1)
+ 1

In my case, Excel 2007, cell A1 is increased to 284 before Excel
"gives up".

I suggest you use radiobuttons instead of checkboxes is you want the
mutual exlusive property. If you insist on using checkboxes, you will
have to try to find the "source" of the click event to see if it is a
mouse click or from user code. I don't know if that is possible in
VBA.

Hope this helps / Lars-Åke
 

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