PC Review


Reply
Thread Tools Rate Thread

Can Checkboxes trigger the Worksheet_Change code?

 
 
Brad E.
Guest
Posts: n/a
 
      8th May 2009
I have developed a spreadsheet with 78 checkboxes on it. I have linked each
checkbox to the cell underneath it. For ease of use for the end-user, I have
a Worksheet_Change code, and the keyboard user can simply enter 1/0, T/F,
True/False or a space to change the checkbox. The Worksheet_Change code
looks at the entry and adjusts the checkbox accordingly. Of course, these
checkboxes also have an effect on other parts of the spreadsheet. Everything
works fine for the keyboard user.

For the mouse-user, though, clicking the checkbox changes the linked-cell
entry, but does not trigger the Worksheet_Change event. Can I assign the
same macro to each checkbox, which will, in turn, edit the linked cell of the
box which was just checked? If so, my question is -- how do I determine the
checkbox number which was just checked, without writing code for each
checkbox? The cell edit (cell.value = cell.value) would then trigger the
Worksheet_Change code, which I know is working correctly.
--
TIA, Brad E.
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      8th May 2009
If you used checkboxes from the Forms toolbar, then you can assign a common
macro to each of the checkboxes.
If you used checkboxes from the control toolbox toolbar, then you can't. You
could create a common procedure that each of the _click subroutines calls,
though.

But I think I would drop the linked cells completely.

This code goes into a General module.

The first routine populates A2:A79 with checkboxes from the Forms toolbar and
names the checkboxes based on its location.

The second routine is the macro that is assigned to each of the checkboxes.

Option Explicit
Sub RunOnce()

Dim CBX As CheckBox
Dim myRange As Range
Dim wks As Worksheet
Dim iRow As Long
Dim FirstRow As Long
Dim LastRow As Long
Dim WhatCol As String

Set wks = ActiveSheet

FirstRow = 2
LastRow = 79
WhatCol = "A"

With wks
'remove any existing checkboxes
.CheckBoxes.Delete

For iRow = FirstRow To LastRow
With .Cells(iRow, WhatCol)
Set CBX = .Parent.CheckBoxes.Add _
(Top:=.Top, _
Left:=.Left, _
Height:=.Height, _
Width:=.Width)
.NumberFormat = ";;;"
End With
With CBX
.Name = "CBX_" & .TopLeftCell.Address(0, 0)
.Caption = ""
.OnAction = "'" & ThisWorkbook.Name & "'!CbxClick"
End With
Next iRow
End With
End Sub
Sub CBXClick()

Dim CBX As CheckBox

Set CBX = ActiveSheet.CheckBoxes(Application.Caller)

Application.EnableEvents = False
ActiveSheet.Range(Mid(CBX.Name, 5)).Value = CBool(CBX.Value = xlOn)
Application.EnableEvents = True

End Sub

========
This code goes behind the worksheet with the checkboxes:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRngToInspect As Range
Dim myIntersect As Range
Dim myCell As Range
Dim CBX As CheckBox

Set myRngToInspect = Me.Range("a2:A79")

Set myIntersect = Intersect(Target, myRngToInspect)

If myIntersect Is Nothing Then
Exit Sub
End If

For Each myCell In myIntersect.Cells
With myCell
Set CBX = Nothing
On Error Resume Next
Set CBX = Me.CheckBoxes("CBX_" & .Address(0, 0))
On Error GoTo 0

If CBX Is Nothing Then
MsgBox "Error in design with: " & .Address
Exit Sub 'get it fixed right away!
End If

Application.EnableEvents = False
Select Case LCase(.Value)
Case Is = " ", lcase("t"), lcase("true")
CBX.Value = xlOn
.Value = True
Case Else
CBX.Value = xlOff
.Value = False
End Select
Application.EnableEvents = True
End With
Next myCell

End Sub

Brad E. wrote:
>
> I have developed a spreadsheet with 78 checkboxes on it. I have linked each
> checkbox to the cell underneath it. For ease of use for the end-user, I have
> a Worksheet_Change code, and the keyboard user can simply enter 1/0, T/F,
> True/False or a space to change the checkbox. The Worksheet_Change code
> looks at the entry and adjusts the checkbox accordingly. Of course, these
> checkboxes also have an effect on other parts of the spreadsheet. Everything
> works fine for the keyboard user.
>
> For the mouse-user, though, clicking the checkbox changes the linked-cell
> entry, but does not trigger the Worksheet_Change event. Can I assign the
> same macro to each checkbox, which will, in turn, edit the linked cell of the
> box which was just checked? If so, my question is -- how do I determine the
> checkbox number which was just checked, without writing code for each
> checkbox? The cell edit (cell.value = cell.value) would then trigger the
> Worksheet_Change code, which I know is working correctly.
> --
> TIA, Brad E.


--

Dave Peterson
 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      8th May 2009
You can call change event code. If you are calling the procedure from outside
of the sheet then you need to change the declaraion of the event code to
public...

Sub test()
Call Worksheet_Change(Range("A1"))
End Sub

Public Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Value
End Sub
--
HTH...

Jim Thomlinson


"Dave Peterson" wrote:

> If you used checkboxes from the Forms toolbar, then you can assign a common
> macro to each of the checkboxes.
> If you used checkboxes from the control toolbox toolbar, then you can't. You
> could create a common procedure that each of the _click subroutines calls,
> though.
>
> But I think I would drop the linked cells completely.
>
> This code goes into a General module.
>
> The first routine populates A2:A79 with checkboxes from the Forms toolbar and
> names the checkboxes based on its location.
>
> The second routine is the macro that is assigned to each of the checkboxes.
>
> Option Explicit
> Sub RunOnce()
>
> Dim CBX As CheckBox
> Dim myRange As Range
> Dim wks As Worksheet
> Dim iRow As Long
> Dim FirstRow As Long
> Dim LastRow As Long
> Dim WhatCol As String
>
> Set wks = ActiveSheet
>
> FirstRow = 2
> LastRow = 79
> WhatCol = "A"
>
> With wks
> 'remove any existing checkboxes
> .CheckBoxes.Delete
>
> For iRow = FirstRow To LastRow
> With .Cells(iRow, WhatCol)
> Set CBX = .Parent.CheckBoxes.Add _
> (Top:=.Top, _
> Left:=.Left, _
> Height:=.Height, _
> Width:=.Width)
> .NumberFormat = ";;;"
> End With
> With CBX
> .Name = "CBX_" & .TopLeftCell.Address(0, 0)
> .Caption = ""
> .OnAction = "'" & ThisWorkbook.Name & "'!CbxClick"
> End With
> Next iRow
> End With
> End Sub
> Sub CBXClick()
>
> Dim CBX As CheckBox
>
> Set CBX = ActiveSheet.CheckBoxes(Application.Caller)
>
> Application.EnableEvents = False
> ActiveSheet.Range(Mid(CBX.Name, 5)).Value = CBool(CBX.Value = xlOn)
> Application.EnableEvents = True
>
> End Sub
>
> ========
> This code goes behind the worksheet with the checkboxes:
>
> Option Explicit
> Private Sub Worksheet_Change(ByVal Target As Range)
> Dim myRngToInspect As Range
> Dim myIntersect As Range
> Dim myCell As Range
> Dim CBX As CheckBox
>
> Set myRngToInspect = Me.Range("a2:A79")
>
> Set myIntersect = Intersect(Target, myRngToInspect)
>
> If myIntersect Is Nothing Then
> Exit Sub
> End If
>
> For Each myCell In myIntersect.Cells
> With myCell
> Set CBX = Nothing
> On Error Resume Next
> Set CBX = Me.CheckBoxes("CBX_" & .Address(0, 0))
> On Error GoTo 0
>
> If CBX Is Nothing Then
> MsgBox "Error in design with: " & .Address
> Exit Sub 'get it fixed right away!
> End If
>
> Application.EnableEvents = False
> Select Case LCase(.Value)
> Case Is = " ", lcase("t"), lcase("true")
> CBX.Value = xlOn
> .Value = True
> Case Else
> CBX.Value = xlOff
> .Value = False
> End Select
> Application.EnableEvents = True
> End With
> Next myCell
>
> End Sub
>
> Brad E. wrote:
> >
> > I have developed a spreadsheet with 78 checkboxes on it. I have linked each
> > checkbox to the cell underneath it. For ease of use for the end-user, I have
> > a Worksheet_Change code, and the keyboard user can simply enter 1/0, T/F,
> > True/False or a space to change the checkbox. The Worksheet_Change code
> > looks at the entry and adjusts the checkbox accordingly. Of course, these
> > checkboxes also have an effect on other parts of the spreadsheet. Everything
> > works fine for the keyboard user.
> >
> > For the mouse-user, though, clicking the checkbox changes the linked-cell
> > entry, but does not trigger the Worksheet_Change event. Can I assign the
> > same macro to each checkbox, which will, in turn, edit the linked cell of the
> > box which was just checked? If so, my question is -- how do I determine the
> > checkbox number which was just checked, without writing code for each
> > checkbox? The cell edit (cell.value = cell.value) would then trigger the
> > Worksheet_Change code, which I know is working correctly.
> > --
> > TIA, Brad E.

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Brad E.
Guest
Posts: n/a
 
      8th May 2009
Thanks, a lot, Dave. Everything works great.
--
Brad E.


"Dave Peterson" wrote:

> If you used checkboxes from the Forms toolbar, then you can assign a common
> macro to each of the checkboxes.
> If you used checkboxes from the control toolbox toolbar, then you can't. You
> could create a common procedure that each of the _click subroutines calls,
> though.
>
> But I think I would drop the linked cells completely.
>
> This code goes into a General module.
>
> The first routine populates A2:A79 with checkboxes from the Forms toolbar and
> names the checkboxes based on its location.
>
> The second routine is the macro that is assigned to each of the checkboxes.
>
> Option Explicit
> Sub RunOnce()
>
> Dim CBX As CheckBox
> Dim myRange As Range
> Dim wks As Worksheet
> Dim iRow As Long
> Dim FirstRow As Long
> Dim LastRow As Long
> Dim WhatCol As String
>
> Set wks = ActiveSheet
>
> FirstRow = 2
> LastRow = 79
> WhatCol = "A"
>
> With wks
> 'remove any existing checkboxes
> .CheckBoxes.Delete
>
> For iRow = FirstRow To LastRow
> With .Cells(iRow, WhatCol)
> Set CBX = .Parent.CheckBoxes.Add _
> (Top:=.Top, _
> Left:=.Left, _
> Height:=.Height, _
> Width:=.Width)
> .NumberFormat = ";;;"
> End With
> With CBX
> .Name = "CBX_" & .TopLeftCell.Address(0, 0)
> .Caption = ""
> .OnAction = "'" & ThisWorkbook.Name & "'!CbxClick"
> End With
> Next iRow
> End With
> End Sub
> Sub CBXClick()
>
> Dim CBX As CheckBox
>
> Set CBX = ActiveSheet.CheckBoxes(Application.Caller)
>
> Application.EnableEvents = False
> ActiveSheet.Range(Mid(CBX.Name, 5)).Value = CBool(CBX.Value = xlOn)
> Application.EnableEvents = True
>
> End Sub
>
> ========
> This code goes behind the worksheet with the checkboxes:
>
> Option Explicit
> Private Sub Worksheet_Change(ByVal Target As Range)
> Dim myRngToInspect As Range
> Dim myIntersect As Range
> Dim myCell As Range
> Dim CBX As CheckBox
>
> Set myRngToInspect = Me.Range("a2:A79")
>
> Set myIntersect = Intersect(Target, myRngToInspect)
>
> If myIntersect Is Nothing Then
> Exit Sub
> End If
>
> For Each myCell In myIntersect.Cells
> With myCell
> Set CBX = Nothing
> On Error Resume Next
> Set CBX = Me.CheckBoxes("CBX_" & .Address(0, 0))
> On Error GoTo 0
>
> If CBX Is Nothing Then
> MsgBox "Error in design with: " & .Address
> Exit Sub 'get it fixed right away!
> End If
>
> Application.EnableEvents = False
> Select Case LCase(.Value)
> Case Is = " ", lcase("t"), lcase("true")
> CBX.Value = xlOn
> .Value = True
> Case Else
> CBX.Value = xlOff
> .Value = False
> End Select
> Application.EnableEvents = True
> End With
> Next myCell
>
> End Sub
>
> Brad E. wrote:
> >
> > I have developed a spreadsheet with 78 checkboxes on it. I have linked each
> > checkbox to the cell underneath it. For ease of use for the end-user, I have
> > a Worksheet_Change code, and the keyboard user can simply enter 1/0, T/F,
> > True/False or a space to change the checkbox. The Worksheet_Change code
> > looks at the entry and adjusts the checkbox accordingly. Of course, these
> > checkboxes also have an effect on other parts of the spreadsheet. Everything
> > works fine for the keyboard user.
> >
> > For the mouse-user, though, clicking the checkbox changes the linked-cell
> > entry, but does not trigger the Worksheet_Change event. Can I assign the
> > same macro to each checkbox, which will, in turn, edit the linked cell of the
> > box which was just checked? If so, my question is -- how do I determine the
> > checkbox number which was just checked, without writing code for each
> > checkbox? The cell edit (cell.value = cell.value) would then trigger the
> > Worksheet_Change code, which I know is working correctly.
> > --
> > TIA, Brad E.

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RAND() Won't Trigger Worksheet_Change egun Microsoft Excel Programming 5 10th Feb 2009 07:01 PM
Run VBA code only worksheet change, but don't trigger worksheet_change event based on what the code does ker_01 Microsoft Excel Programming 6 3rd Oct 2008 09:45 PM
Trigger Macro by Worksheet_Change JSnow Microsoft Excel Misc 5 2nd Oct 2008 06:27 PM
Worksheet_Change wont Trigger Scorcel Microsoft Excel Programming 1 1st Sep 2004 06:43 AM
Worksheet_Change sub does not trigger a called macro herman Microsoft Excel Programming 3 8th Feb 2004 08:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:20 PM.