0

I am trying to create a macro that cycles through different formats by simply pressing its corresponding shortcut. The code I created is the following:

Option Explicit

Sub FormatCycle()
'
' FormatCycle Macro
'
' Keyboard Shortcut: Ctrl+Shift+E
'

If Selection.NumberFormat = "General" Then
    Selection.NumberFormat = "#,##0.00_);(#,##0.00)"
ElseIf Selection.NumberFormat = "#,##0.00_);(#,##0.00)" Then
    Selection.NumberFormat = "0.00%_);(0.00%)"
ElseIf Selection.NumberFormat = "0.00%_);(0.00%)" Then
    Selection.NumberFormat = "#,##0.00""x"";(#,##0.00""x"")"
ElseIf Selection.NumberFormat = "#,##0.00""x"";(#,##0.00""x"")" Then
    Selection.NumberFormat = "General"
Else
    Selection.NumberFormat = "General"
End If

End Sub

Everything works fine except for the second type of format with negative values (i.e. (#,##0.00)). When I use this format on its own, e.g. by custom formatting a generic cell, it does its job and displays the negative number in brackets. However, within the macro, when I make it run it shows the number with the "general" format, e.g. -12.00 instead of (12.00).

What I am getting wrong?

1
  • For negative numbers to display in parenthesis, try wrapping in quotation marks, like this NumberFormat = "0;(""$0,000"")" Commented Jul 7, 2019 at 14:56

2 Answers 2

0

For negative numbers to display in parenthesis, try wrapping in quotation marks, like this NumberFormat = "0;(""$0,000"")" 

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Jenn, thanks for your reply. Unfortunately, this does not work: by using the quotation marks as you suggested, Excel recognises the content in the quotation mark as a string and the output is then simply (#,##0.00).
0

I too was seeing -12.00 with Selection.NumberFormat, but began seeing (12.00) once I switched to Selection.NumberFormatLocal. I'd therefore suggest replacing Selection.NumberFormat in your code with Selection.NumberFormatLocal.

Something like:

Option Explicit

Sub FormatCycle()
'
' FormatCycle Macro
'
' Keyboard Shortcut: Ctrl+Shift+E
'
    Dim currentCell As Range
    On Error Resume Next
    Set currentCell = Selection
    On Error GoTo 0

    If currentCell Is Nothing Then Exit Sub ' Could probably check if TypeName is a range

    With currentCell
        Select Case .NumberFormatLocal
            Case "General"
                .NumberFormatLocal = "#,##0.00_);(#,##0.00)"

            Case "#,##0.00_);(#,##0.00)"
                .NumberFormatLocal = "0.00%_);(0.00%)"

            Case "0.00%_);(0.00%)"
                .NumberFormatLocal = "#,##0.00""x"";(#,##0.00""x"")"

            Case "#,##0.00""x"";(#,##0.00""x"")"
                .NumberFormatLocal = "General"

            Case Else
                .NumberFormatLocal = "General"
        End Select
    End With

End Sub

The other thing is that you are effectively looping through a predefined list of number formats (each branch in your IF statement is what was assigned in the previous branch). Therefore, it might be better to store all number formats that you want to loop through in an array, then look up the position of the current number format (in the array) and return the number format which follows it. That way if you want to introduce more number formats into the cycle, instead of manually adding more If Else or Case branches, you'd only need to add the new entry(s) to the array.

On the other hand, if your current approach is fine and this is just a quick macro, then no need to fix what's not broken.

5 Comments

Hi Chillin, thanks for your reply. I tried your code, but unfortunately nothing happens (literally). As for your suggestion to use arrays, do you mean something of this kind? ``` Sub NewFormat() ' ' NewFormat Macro ' ' Keyboard Shortcut: Ctrl+Shift+T Dim x, myFormats myFormats = Array("#,##0.00;(#,##0.00)", "0.00%;(0.00%)", "0.00""x"";(0.00""x"")") x = Application.Match(Selection.NumberFormat, myFormats, False) If IsError(x) Then x = 0 Selection.NumberFormat = myFormats((x Mod (UBound(myFormats) + 1))) End Sub ```
@albfas, I tested it on my end before submitting and it was working for me (the selected cell's number formatting was changing every time the code was executed). I recommend selecting the cell you want to change, then step through each line of the code with F8 key -- and see why it's doing nothing.
Also, the code in your comment is roughly what I meant, yes.
I get this error message: "run-time error '1004': unable to set the NumberFormatLocal property of the Range class". The debug tool highlights the following line of code " .NumberFormatLocal = "General"". P.S. I am a VBA beginner, can't really figure out what's going wrong.
Update: I used your code, with the following changes and it worked so thanks a lot! 1) didn't use "on error" functions and "if current cell is nothing..." line 2) I used .NumberFormat everywhere, except for the format where I was having trouble, for which I used .NumberFormatLocal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.