• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Assign 2 macros to a checkbox

Dokat

Member
Hi,

I's like to assign 2 macros to a checkbox. WHen i click on the checkbox it only lets me run one macro.

Can you please help.

Here is the 2 macro codes i used

Code:
Option Explicit
Sub testme()
Dim ThisCBX As CheckBox
Dim CBX As CheckBox
Set ThisCBX = ActiveSheet.CheckBoxes(Application.Caller)
If ThisCBX.Value = xlOn Then
For Each CBX In ActiveSheet.CheckBoxes
If CBX.Name = ThisCBX.Name Then
'do nothing
Else
CBX.Value = xlOff
End If
Next CBX
End If
End Sub
Sub CheckBox2()
'Sub sbSortDataInExcelInDescendingOrder()
Dim strDataRange, strkeyRange As String
strDataRange = "C2:F13"
strkeyRange = "d3:d13"
With Sheets("Sheet2").Sort
.SortFields.Clear
.SortFields.Add _
Key:=Range(strkeyRange), _
SortOn:=xlSortOnValues, _
Order:=xlDescending, _
DataOption:=xlSortNormal
.SetRange Range(strDataRange)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Thanks
 

Attachments

  • Test.xlsm
    32.2 KB · Views: 5
you can do 1 of 2 things

Firstly, Combine the code together in one macro

Code:
Sub CheckBox2()

'Setup Check Boxes
Dim ThisCBX As CheckBox
Dim CBX As CheckBox

Set ThisCBX = ActiveSheet.CheckBoxes(Application.Caller)

If ThisCBX.Value = xlOn Then
  For Each CBX In ActiveSheet.CheckBoxes
    If CBX.Name <> ThisCBX.Name Then CBX.Value = xlOff
  Next CBX
End If

'Sort data
Dim strDataRange, strkeyRange As String
strDataRange = "C2:F13"
strkeyRange = "d3:d13"
With Sheets("Sheet2").Sort
    .SortFields.Clear
    .SortFields.Add _
    Key:=Range(strkeyRange), _
    SortOn:=xlSortOnValues, _
    Order:=xlDescending, _
    DataOption:=xlSortNormal
    .SetRange Range(strDataRange)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

End Sub

The second method is to simply call each from a third macro

Code:
Sub CheckBox2()
    Call Sort
    Call sbSortDataInExcelInDescendingOrder
End Sub

Sub Sort()
Dim ThisCBX As CheckBox
Dim CBX As CheckBox

Set ThisCBX = ActiveSheet.CheckBoxes(Application.Caller)

If ThisCBX.Value = xlOn Then
  For Each CBX In ActiveSheet.CheckBoxes
      If CBX.Name <> ThisCBX.Name Then CBX.Value = xlOff
  Next CBX
End If
End Sub

Sub sbSortDataInExcelInDescendingOrder()
Dim strDataRange, strkeyRange As String
strDataRange = "C2:F13"
strkeyRange = "d3:d13"
With Sheets("Sheet2").Sort
    .SortFields.Clear
    .SortFields.Add _
    Key:=Range(strkeyRange), _
    SortOn:=xlSortOnValues, _
    Order:=xlDescending, _
    DataOption:=xlSortNormal
    .SetRange Range(strDataRange)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
 
Back
Top