Format toggle issue

kkonen

New Member
Joined
Feb 19, 2010
Messages
3
I am tryin g to write code that will toggle through number formats. I have tried using if-else and case select methods. They behave differently, but neither work. If-else toggles between the first format and General. The Case method always sets to the percentage type and doesn't toggle.

Both codes are below, with the if-else commented out.

Thanks!

VBA Code:
Public Sub Numberformat_ToggleDo()
Dim WorkRng As Range

On Error Resume Next

Set WorkRng = Application.Selection

Select Case WorkRng.Cells(1, 1).NumberFormat
    Case "General": WorkRng.NumberFormat = "#,##0.0_);(#,##0.0)"
    Case "#,##0.0_);(#,##0.0)": WorkRng.NumberFormat = "_($* #,##0.0_);_($* (#,##0.0);_($* " - "?_);_(@_)"
    Case "_($* #,##0.0_);_($* (#,##0.0);_($* " - "?_);_(@_)": WorkRng.NumberFormat = "#,##0.0%_);(#,##0.0%)"
    Case "#,##0.0%_);(#,##0.0%)": WorkRng.NumberFormat = "#,##0.0x"
    Case Else: WorkRng.NumberFormat = "General"
End Select

'If WorkRng.Cells(1, 1).NumberFormat = "#,##0.0_);(#,##0.0)" Then
'    WorkRng.NumberFormat = "_($* #,##0.0_);_($* (#,##0.0);_($* " - "?_);_(@_)"
'ElseIf WorkRng.Cells(1, 1).NumberFormat = "_($* #,##0.0_);_($* (#,##0.0);_($* " - "?_);_(@_)" Then
'    WorkRng.NumberFormat = "#,##0.0%_);(#,##0.0%)"
'ElseIf WorkRng.Cells(1, 1).NumberFormat = "#,##0.0%_);(#,##0.0%)" Then
'    WorkRng.NumberFormat = "#,##0.0x"
'ElseIf WorkRng.Cells(1, 1).NumberFormat = "#,##0.0x" Then
'    WorkRng.NumberFormat = "General"
'Else
'    WorkRng.NumberFormat = "#,##0.0_);(#,##0.0)"
'End If
On Error GoTo 0
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hello, check this: :)
VBA Code:
Public Sub Numberformat_ToggleDo()
Dim WorkRng As Range

On Error Resume Next

Set WorkRng = Application.Selection

Select Case WorkRng.Cells(1, 1).NumberFormat
    Case "General"
    WorkRng.NumberFormat = "#,##0.0_);(#,##0.0)"
    
    Case "#,##0.0_);(#,##0.0)"
    WorkRng.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""?_);_(@_)"
    
    Case "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""?_);_(@_)"
    WorkRng.NumberFormat = "#,##0.0%_);(#,##0.0%)"
    
    Case "#,##0.0%_);(#,##0.0%)"
    WorkRng.NumberFormat = "#,##0.0x"
    
    Case Else
    WorkRng.NumberFormat = "General"
End Select
On Error GoTo 0
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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