Help with Case Selection changing number formats

moliver

New Member
Joined
Aug 7, 2008
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
Hi Forum,

I created a case selection to change number format by pressing at shortcut so the selection will change from Comma to Percent to Currency and then back to Comma. Code below.

The problem is when I select more than one cell and one of the cell in the selection has a different format from the other I get the following error:
"Run Time Error 91"
"Object variable or With block variable not set"
the line that I get highlighted is:
Code:
Select Case .Style
Any ideas how to fix it?

Code:
Sub Change_Format_Number()
 With Selection
    Select Case .Style
    Case "Comma"
    .Style = "Percent"
    
    Case "Percent"
    .Style = "Currency"
    
    Case "Currency"
    .Style = "Comma"
        
    Case Else
    .Style = "Comma"
    End Select
 End With
End Sub
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You need to loop through each cell in your selection.
Try this:
Code:
Sub Change_Format_Number()
 Dim cell As Range
 For Each cell In Selection
    With cell
        Select Case .Style
            Case "Comma"
                .Style = "Percent"
    
            Case "Percent"
                .Style = "Currency"
    
            Case "Currency"
                .Style = "Comma"
            
            Case Else
                .Style = "Comma"
        End Select
    End With
 Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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