VBA problem with decimals and concatenation

DBrian

New Member
Joined
Jan 5, 2012
Messages
16
I have a range of numbers in cells that have General formatting. When a number in a cell is less than 43560, for example 40000, then I want to add the words "square feet", so it displays as "40000 square feet".

I accomplish this with the following:
If Val(ActiveCell) < 43560 Then ActiveCell = ActiveCell & " square feet"
The result is "40000 square feet". So far, no problem.

The problem comes when the number is greater than or equal to 43560, in which case I want to divide the number by 43560, and I want the number to display rounded to two decimal places followed by the word "acre". For example, if the number is 46000 I want it to display as "1.06 acre".

I tried accomplishing this in two steps:
If Val(ActiveCell) >= 43560 Then ActiveCell.NumberFormat = "0.00"
If Val(ActiveCell) >= 43560 Then ActiveCell = Val(ActiveCell) / 43560 & " acre"

But instead of a number with two decimal places, the result is this: 1.05601469237833 acre

An additional problem arises when the number in the cell is exactly 43560. Then the result is this: 1 acre
The result I wanted was "1.00 acre".

Any help is greatly appreciated.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
try
If Val(ActiveCell) >= 43560 Then ActiveCell = Format(Val(ActiveCell) / 43560, "#0.00") & " acre"
 
Upvote 0
Hi Brian,

Maybe try using Format function:

Code:
    If ActiveCell >= 43560 Then
        ActiveCell = Format(ActiveCell / 43560, "0.00") & " acre"
    End If

Edit: sorry Fluff, just noticed your post!
 
Last edited:
Upvote 0
Or

Code:
ActiveCell = Application.Round(ActiveCell.Value / 43560, 2) & " acre"
 
Upvote 0
Or if you want to just truncated the value instead of rounding then
Code:
ActiveCell = Left(ActiveCell.Value / 43560, InStr(ActiveCell.Value / 43560, ".") + 2) & " acre"
But this could again leave the value at 1 if it is exactly divisible.
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,671
Messages
6,132,042
Members
449,697
Latest member
bororob85

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