APC Australia

Conditiona­l formatting using macros in Excel

Helen Bradley explains how to apply multiple conditiona­l formats to a single range in Excel using VBA.

-

When you use conditiona­l formats to highlight informatio­n in a worksheet, you can only use one set of formats at a time. If you want your user to be able to switch between formats, you’ll need to build a solution using VBA. This month, we look at a way to define multiple conditiona­l formats for a single range of cells and apply them as and when required using worksheet buttons.

OUR CONDITIONA­L FORMATS

We’ll use four formats — one to identify entries that are weekend days, one that highlights higher-than-average values and one that highlights alternate rows. The final one will allow us to highlight informatio­n relating to a manager chosen from a dropdown list we’ve placed in cell F3. Each conditiona­l format is based on a formula, the data range is A3:C33 and these are the formulae: =WEEKDAY($A3,2)>=6 =$B3>AVERAGE($B$3:$B$33) =mod(row(),2)=0 =$C3=$F$3

THE MACRO SOLUTION

In the macro solution, each format will be accessed via a CommandBut­ton, which is added to the sheet via the Developer tab. This is the code for each of the four buttons — the code appears in the Sheet code area:

Private Sub CommandBut­ton1_ Click() Dim formatCode As Integer formatCode = 1 applyForma­t (formatCode) End Sub

Private Sub CommandBut­ton2_ Click() Dim formatCode As Integer formatCode = 2 applyForma­t (formatCode) End Sub

Private Sub CommandBut­ton3_ Click() Dim formatCode As Integer formatCode = 3 applyForma­t (formatCode) End Sub Private Sub CommandBut­ton4_ Click() Dim formatCode As Integer formatCode = 4 applyForma­t (formatCode) End Sub

Each button calls a routine called applyForma­t which handles applying the Conditiona­l format depending on the value of a variable, formatCode, sent to that routine. In this way, one procedure can handle all the formatting. This is the procedure applyForma­t — the code appears in workbook module:

Sub applyForma­t(code As Integer)

Dim userRange As Range, userFormat As FormatCond­ition

Set userRange = Range(“=$a$3:$c$33”)

Range(“f:f”).EntireColu­mn. Hidden = True

If userRange. FormatCond­itions.Count <> 0 Then

userRange.FormatCond­itions. Delete End If Select Case code Case Is = 1

Set userFormat = userRange.FormatCond­itions. Add(xlExpressi­on, xlFormula, “=$B3>AVERAGE($B$3:$B$33)”)

userFormat.Interior. ThemeColor = xlThemeCol­orAccent2 Case Is = 2

Set userFormat = userRange.FormatCond­itions. Add(xlExpressi­on, xlFormula, “=WEEKDAY($A3,2)>=6”)

userFormat.Interior. ThemeColor = xlThemeCol­orAccent3 Case Is = 3

Set userFormat = userRange.FormatCond­itions. Add(xlExpressi­on, xlFormula, “=mod(row(),2)=0”)

userFormat.Interior. ThemeColor = xlThemeCol­orAccent4 Case Is = 4

Range(“f:f”). EntireColu­mn.Hidden = False

Set userFormat = userRange.FormatCond­itions. Add(xlExpressi­on, xlFormula, “=$C3=$F$3”)

userFormat.Interior. ThemeColor = xlThemeCol­orAccent5 End Select

userFormat.Interior. TintAndSha­de = 0.8

End Sub

The macro removes any format already in place and it hides column F. The appropriat­e conditiona­l format is then added and a theme colour specified for the cells. If the fourth button is selected, column F is made visible to give the user access to the dropdown list in that column.

 ??  ?? Download the macros for this story at www.apcmag. com/magstuff. When each CommandBut­ton is pressed, a different format is applied. The last CommandBut­ton reveals the dropdown list in column F.
Download the macros for this story at www.apcmag. com/magstuff. When each CommandBut­ton is pressed, a different format is applied. The last CommandBut­ton reveals the dropdown list in column F.

Newspapers in English

Newspapers from Australia