Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Format all numbers with NumberFormat '0.0' using VBA

Posted on 2011-09-02
5
Medium Priority
?
496 Views
Last Modified: 2012-05-12
Dear Experts:

On the active worksheet I would like to search for ALL numeric values (even if they have been entered as text) and apply the following format: NumberFormat '0.0', ie. format all numeric values as numbers with one decimal place.  

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 2000 total points
ID: 36475726
Try this,

Thomas
Sub agdsagds()
Dim cl As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False


For Each cl In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
    If IsNumeric(cl) Then
        cl.NumberFormat = "0.0"
        cl.Value = CDbl(cl.Value)
    End If
Next

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True

End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 36476231
Hi Thomas,

great, thank you very much for your professional help. One more thing, how would this macro to be tweaked if not the used range would be used but a workbook level scope name, named 'MyRange'

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
 
LVL 39

Assisted Solution

by:nutsch
nutsch earned 2000 total points
ID: 36476236
You'd replace

For Each cl In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)

by

For Each cl In activeworkbook.range("MyRange").SpecialCells(xlCellTypeConstants)

Thomas
0
 

Author Closing Comment

by:AndreasHermle
ID: 36476262
Great, Thomas. I really appreciate  you swift and professional support.

Thank you very much. Regards, Andreas
0
 
LVL 39

Expert Comment

by:nutsch
ID: 36476270
Glad to assist. Thanks for the kind words.

Thomas
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question