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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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