Macro to toggle number formats

bferg

New Member
Joined
Mar 12, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a macro that will toggle through the following year formats - 2021, 2021A (actual), 2021E (expected)

I have used similar VBA to create macros to toggle number formats but the below VBA is not working. The initial change from whatever format to 2021A works but if I use the macro again, I do not get to 2021E. I appreciate any help on what I am doing incorrectly here. Thank you!

VBA code:

1615603393189.png


Sub YearToggle()
'
' YearToggle Macro
'
' Keyboard Shortcut: Ctrl+g
'
Select Case Selection.NumberFormat

Case "#A"
Selection.NumberFormat = "#E"

Case "#E"
Selection.NumberFormat = "#"

Case Else
Selection.NumberFormat = "#A"

End Select

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
VBA Code:
Sub YearToggle()
'
' YearToggle Macro
'
' Keyboard Shortcut: Ctrl+g
'2021

Select Case Selection.NumberFormat

Case "#""A"""
Selection.NumberFormat = "#""E"""

Case "#""E"""
Selection.NumberFormat = "#"

Case Else
Selection.NumberFormat = "#""A"""

End Select

End Sub
 
Upvote 0
Solution
VBA Code:
Sub YearToggle()
'
' YearToggle Macro
'
' Keyboard Shortcut: Ctrl+g
'2021

Select Case Selection.NumberFormat

Case "#""A"""
Selection.NumberFormat = "#""E"""

Case "#""E"""
Selection.NumberFormat = "#"

Case Else
Selection.NumberFormat = "#""A"""

End Select

End Sub
Thank you!
 
Upvote 0
Alternative :
VBA Code:
Sub YearToggle()
'
' YearToggle Macro
'
' Keyboard Shortcut: Ctrl+g
'2021

Select Case Selection.NumberFormat

Case "#\A"
Selection.NumberFormat = "#\E"

Case "#E"
Selection.NumberFormat = "#"

Case Else
Selection.NumberFormat = "#\A"

End Select

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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