[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

convert numbers to text with 2 decimal places

Posted on 2014-08-08
13
Medium Priority
?
197 Views
Last Modified: 2014-08-08
I need help with converting numbers to text numbers with 2 decimal places.

In my file in cells I see 100.20 but in the formula bar I see 100.2

so if I have 150.5 I need it to be 150.50 if I have 100.2 I need it to be 100.20

Thanks in advance
0
Comment
Question by:Jagwarman
  • 7
  • 6
13 Comments
 

Author Comment

by:Jagwarman
ID: 40249176
Should have said this needs to be using VBA code.

Thanks
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40249259
In VBA:
    ActiveCell.NumberFormat = "@"
    ActiveCell.Value = Format(Val(ActiveCell.Value), "0.00")

Open in new window


The first line changes the cell format to text; the second line adds any necessary decimals

Regards,
-Glenn
0
 

Author Comment

by:Jagwarman
ID: 40249492
Thanks Glenn
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40249592
Thank me when you've got it working!  Please click the "Accept this solution" above my previous post to properly close the question.
Thanks,
Glenn
0
 

Author Comment

by:Jagwarman
ID: 40249650
good point cos I can't get it to work I need to get it to work over a range F:F and sometime F:G I thought maybe I could refer to your earlier post where you solve the date for text,  and work out from that, but it never is that simple. So I am sorry but I need more of your help
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40249664
Kind of figured that.  Here's a more-general code that assumes a range of values is selected in advance:
Sub Convert_To_2Decimal()
    Dim rng As Range
    Dim cl As Object
    
    Set rng = Selection
    
    For Each cl In rng
        cl.NumberFormat = "@"
        cl.Value = Format(Val(cl.Value), "0.00")
    Next cl
End Sub

Open in new window


Select the range you want to convert, then while it's selected, run this macro.

-Glenn
0
 

Author Comment

by:Jagwarman
ID: 40249689
Hmm I set the range F:F and row 1 was a header.

It changed the header to 0.00 and every cell to the end of the file in row F:F
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40249711
Hmm, well, you should only select the cells that you want to change! :-)

So, I now presume - although you did not originally state it - that you only want this change to occur:
1) if the value contains a number, and
2) if the value is not blank.

Please confirm this and add any additional clarification necessary to provide a workable solution.

-Glenn
0
 

Author Comment

by:Jagwarman
ID: 40249723
Sorry Glenn.

Yes I only want the change to occur in in the range F:F
that contain a number and the value is not blank
Many thanks
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 2000 total points
ID: 40249733
Okay, try this code instead.
Sub Convert_To_2Decimal()
    Dim rng As Range
    Dim cl As Object
    
    Set rng = Range("F1:F" & Cells.SpecialCells(xlLastCell).Row)
    
    For Each cl In rng
        If WorksheetFunction.IsNumber(cl) Then
            cl.NumberFormat = "@"
            cl.Value = Format(Val(cl.Value), "0.00")
        End If
    Next cl
End Sub

Open in new window


It is very specific:  it ONLY changes the numerical values in column F from the first cell down to the last cell used.  You don't have to select the range; it does it for you.  You really don't want to select the entire column as that takes more time (processing cells with no values well past your data range).

-Glenn
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40249739
You mentioned column G earlier; if you need to change that column also, then modify line 5 like so:
Set rng = Range("G1:G" & Cells.SpecialCells(xlLastCell).Row)

Open in new window

0
 

Author Comment

by:Jagwarman
ID: 40249816
wow, thanks Gelnn that is brilliant. I can now have a restful weekend without worrying about this. Thank you so much. Have a great weekend.

If you were local the meal and wine would be on me :-)
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40249929
I'm glad I could help.  Sorry it took so long to get a solution to you.

Have a good weekend,
-Glenn
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

834 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