VBA Remove decimal places if they don't exist

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
790
Office Version
  1. 365
Platform
  1. Windows
Hi fellas, quick one I hope.

Is there a non Conditional Formatting way of representing text like this:

£299
£189.99
£250
£399.99


So if the number has a decimal place, give it one, otherwise don't display it.

Basically I don't want to see this: £299.00

Cheers!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I think the closest you are going to get is to use a custom format where you change the fixed decimal place holders from .00 to .??
eg from [$£-en-GB]#,##0.00 to [$£-en-GB]#,##0.?? I am in Australia in the UK you might only see the £ symbol.
Unfortunately it will still show the decimal point even when its .00
eg £299. & £250.

1615461137149.png
 
Upvote 0
In that case I may have to look in to an evaluation function where it decides how to format on a case-by-case basis.
 
Upvote 0
you asked for a VBA solution, so you could just choose which format you need, row by row using
VBA Code:
if Int(MyValue) = MyValue then
    Cells(  ,  ) = Format(MyValue , "£0")
else
    Cells(  ,  ) = Format(MyValue, "£0.00")
end if
 
Upvote 0
Solution
you asked for a VBA solution, so you could just choose which format you need, row by row using
VBA Code:
if Int(MyValue) = MyValue then
    Cells(  ,  ) = Format(MyValue , "£0")
else
    Cells(  ,  ) = Format(MyValue, "£0.00")
end if

Hi bud, I have this:

VBA Code:
Range("AD2").Activate
x = 1

Dim rng As Range
Dim cl As Range

For x = 1 To 10

    Set rng = Range(Cells(2, ActiveCell.Column), Cells(lastrow, ActiveCell.Column))
   
    For Each cl In rng
    If Int(cl) Then
    cl.NumberFormat = "£0"
    Else
    cl.NumberFormat = "£0.00"
    End If
    Next cl
ActiveCell.Offset(0, 6).Activate
Next x

Which is built around your logic.

We have price in 10 columns spaced every 6 columns apart. So I'm hoping to just loop 10 times to change the whole column.

This does happily convert whole integers into a currency format with no decimal, but it also changes £269.99 to £270 which is no good.

Any help? Thanks.

EDIT: Nevermind, I missed the part of where your formula basically says "If Integer of £229.99 = £229.99 then"

So I've got it with the below:

VBA Code:
For x = 1 To 10

    Set rng = Range(Cells(2, ActiveCell.Column), Cells(lastrow, ActiveCell.Column))
    
    For Each cl In rng
    If Int(cl) = cl Then
    cl.NumberFormat = "£0"
    Else
    cl.NumberFormat = "£0.00"
    End If
    Next cl
ActiveCell.Offset(0, 6).Activate
Next x

Thanks!
 
Upvote 0
try this
VBA Code:
if cl.value - Int(cl.value)<.0001 then
 
Upvote 0
I would suggest that the values will look much better if they line up and have commas in for thousands and to do that:

In the IF being TRUE, I would replace the
cl.NumberFormat = "£0"
with
cl.NumberFormat = "£#,##0_._#_#"

and if you want £250.50 to appear as £250.5
then if the IF being FALSE replace
cl.NumberFormat = "£0.00"
with
cl.NumberFormat = "£#,##0.??"

1615467840439.png
 
Upvote 0
Hi Alex,

Appreciate the input but that's not necessary, the output for this is a mail-merged document so the mail merge takes the explicit cell value as it appears, which is why I need something with the pound sign and not just the number format. We are offering products to customers and the price must say "£399.99" or "£280" - having something like £275.5" is not proper punctuation and then having gaps or false spaces causes more problems. Cheers.
 
Upvote 0
Hi Alex,

Appreciate the input but that's not necessary, the output for this is a mail-merged document so the mail merge takes the explicit cell value as it appears, which is why I need something with the pound sign and not just the number format. We are offering products to customers and the price must say "£399.99" or "£280" - having something like £275.5" is not proper punctuation and then having gaps or false spaces causes more problems. Cheers.
Thanks for the update.
 
Upvote 0

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

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