New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Shortcut Macro to Toggle between multiple Macros

Macros, VBA, Excel Automation, etc.

Shortcut Macro to Toggle between multiple Macros

Postby NewbietoVBA » Thu Oct 01, 2015 11:01 am

Hello,

I am looking to create a macro that will allow me to toggle between a range of background colors of a selected range of cells, using the same key stroke, but run them separately, and run them in a loop. For example, I would like to hit Ctrl + Shift + C with the following outputs: First Time = Dark Blue, Second Time = Light Blue, Third Time = Grey, Fourth Time = Yellow, Fifth Time = No Fill, Nth Time = keep togging through.

I can get it all to run through once, but I do not know how to have them run separately, using the same key strokes, and toggle through each case.

Any help is very much appreciated!

Code Is below

Code: Select all
Sub AllCellFill()
CellFill1
CellFill2
CellFill3
CellFill4
CellFill5
End Sub
Sub CellFill1()
With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub
Sub CellFill2()
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.249977111117893
        .PatternTintAndShade = 0
    End With
End Sub

Sub CellFill3()
With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent1
        .TintAndShade = 0.599993896298105
        .PatternTintAndShade = 0
    End With
End Sub
Sub CellFill4()
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

End Sub
Sub CellFill5()
 With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub
  • 0

NewbietoVBA
Rookie
 
Posts: 2
Joined: Oct 1, 2015
Reputation: 0
Excel Version: 2013

Re: Shortcut Macro to Toggle between multiple Macros

Postby NoSparks » Thu Oct 01, 2015 12:15 pm

Use a global variable for this. The value will be maintained until you quit Excel.

If you need the color preserved over openning and closing of the workbook save the number to a cell somewhere with the workbook close event and have the work book open event make the variable equal to that cell value when the workbook is reopenned.

Code: Select all
Option Explicit
    Dim WhichSub As Integer

Sub AllCellFill()
    Dim WhatSub As String
   
WhichSub = WhichSub + 1
If WhichSub = 6 Then WhichSub = 1
WhatSub = "CellFill" & WhichSub
Run WhatSub

    'CellFill1
    'CellFill2
    'CellFill3
    'CellFill4
    'CellFill5

End Sub
  • 0

NoSparks
Excel Hobbyist
 
Posts: 637
Joined: May 28, 2014
Reputation: 103
Excel Version: 2010

Re: Shortcut Macro to Toggle between multiple Macros

Postby NewbietoVBA » Sat Oct 03, 2015 8:51 am

Thank you! I will try this. Hope it works! Fingers crossed!
  • 0

NewbietoVBA
Rookie
 
Posts: 2
Joined: Oct 1, 2015
Reputation: 0
Excel Version: 2013


Return to Macros and VBA Questions

Who is online

Users browsing this forum: Google [Bot] and 214 guests