VBA Number Format Toggle

coinbank2

New Member
Joined
Mar 15, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I am trying to do a toggle shortcut key for number formats. I have come up with the below but the only issues is if the existing number format is not one of the below it will not start the toggle. I keep getting an error "Case Mismatch on the 2nd Case (after the "Case All")

Sub AlexNum()

Select Case Selection.NumberFormat
Case All
Selection.NumberFormat = "(* #,##0.00);(* (#,##0.00);(* " - "??);(@)"
Case "(* #,##0.00);(* (#,##0.00);(* " - "??);(@)": Selection.NumberFormat = "($* #,##0.00);(*$ (#,##0.00);(*$ " - "??);($@)"
Case "($* #,##0.00);(*$ (#,##0.00);(*$ " - "??);($@)": Selection.NumberFormat = "#,##0%"
Case "#,##0%": Selection.NumberFormat = "###,###x"
Case "###,###x": Selection.NumberFormat = "mm/dd/yyyy"
Case "mm/dd/yyyy": Selection.NumberFormat = "mmm-yyyy"
Case "mmm-yyyy": Selection.NumberFormat = "General"
Case Else: Selection.NumberFormat = "General"
End Select
End Sub

Basically i need this macro toggle to work regardless of what format is set in the cell which is why i needed the "Case All". Any help out there
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Don't see how this can not raise an error after Case All (which itself should raise an error unless it's a module level variable) because it looks like a line label or a statement:

Selection.NumberFormat = "(* #,##0.00);(* (#,##0.00);(* " - "??);(@)"

This is what I got in the immediate window for a cell formatted as currency

?selection.numberformat
$#,##0.00_);[Red]($#,##0.00)
 
Last edited:
Upvote 0
just noticed your "-"
You need to escape your double quotes. Try ""-"". Sometimes it's """-"""
That's why it looked like a line label/statement to vba.
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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