Toggle button to run two Macros

benjy79

New Member
Joined
Apr 18, 2011
Messages
2
I have a spreadsheet that I put two buttons on. One button runs a macro that conditionally formats dates that will expire in next year and other runs a macro to undo that formating. What I want to do is have just one button - a toggle button that when clicked once shows expirations in red and clicked again will go back the way it was.

I want button to say "Check Experations" to start. Then when clicked makes dates coming in next year red.

The button will then read "Remove Check" So user will click to get rid of the red dates.


I have this as code for first button:

Sub CheckExpireations()
'
' CheckExpireations Macro
'
' Keyboard Shortcut: Ctrl+y
'
Range("D9:AC24").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=$AF$1+365"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
End Sub




And this for code for second button(macro)

Sub Uncheck()
'
' Uncheck Macro
'
' Keyboard Shortcut: Ctrl+r
'
ActiveWindow.ScrollRow = 11
ActiveWindow.ScrollRow = 9
Range("D9:AC24").Select
Selection.FormatConditions.Delete
End Sub


So in short toggle button shows "Check Experations" if Clicked runs macro CheckExperations(). Then button shows "Remove Check" and when clicked runs macro Uncheck()

Thanks for your help


 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
make two buttons. and move over each other. When you click on one, button1.visible = false, button2.visible = true. When you click the other then, button1.visible = true, and vice versa.
 
Upvote 0
Welcome to the board.
Using an ActiveX CommandButton, try this:
Code:
Private Sub CommandButton1_Click()
  Select Case CommandButton1.Caption
    Case "Check Expirations"
      Range("D9:AC24").Select
      Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
      Formula1:="=$AF$1+365"
      Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
      With Selection.FormatConditions(1).Font
      .Color = -16776961
      .TintAndShade = 0
      End With
      Selection.FormatConditions(1).StopIfTrue = True
      CommandButton1.Caption = "Remove Check"
    Case "Remove Check"
      ActiveWindow.ScrollRow = 11
      ActiveWindow.ScrollRow = 9
      Range("D9:AC24").Select
      Selection.FormatConditions.Delete
      CommandButton1.Caption = "Check Expirations"
  End Select
End Sub
When you first setup the button, be sure to manually set the caption to one or the other.
This code goes in your Sheet Module. You could also toggle the color for more of a visual cue.
 
Last edited:
Upvote 0
Thanks Warship that worked. How would I change the color? I changed in properties but the button was same color on Check Experations and remove Check.
 
Upvote 0
Code:
Private Sub CommandButton1_Click()
  Select Case CommandButton1.Caption
    Case "Check Expirations"
      Range("D9:AC24").Select
      Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
      Formula1:="=$AF$1+365"
      Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
      With Selection.FormatConditions(1).Font
      .Color = -16776961
      .TintAndShade = 0
      End With
      Selection.FormatConditions(1).StopIfTrue = True
      CommandButton1.Caption = "Remove Check"
      CommandButton1.BackColor = &H8080FF
    Case "Remove Check"
      ActiveWindow.ScrollRow = 11
      ActiveWindow.ScrollRow = 9
      Range("D9:AC24").Select
      Selection.FormatConditions.Delete
      CommandButton1.Caption = "Check Expirations"
      CommandButton1.BackColor = &HC0FFC0
  End Select
End Sub

To pick the colors you want:
Go into button properties and manually change the color.
Copy the number that shows after selection, i.e. &H00C0FFC0&
Paste this into your VBA code after: CommandButton1.BackColor =
After pasting, the number will change to the way Excel wants it.
Repeat for your second color choice.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,492
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top