Format decimal place for variable

boileauj

New Member
Joined
Aug 12, 2009
Messages
12
I have declared a variable 'Z' in a procedure I am running, I want it to be displayed in a MsgBox with as a percentage with two decimal places.

The code I have is:

Dim Z As Variant

MsgBox "The Homogeneity of the Batch Model is:" & vbCrLf & vbCrLf & _
" " & Z & "%", vbOKOnly, "Homogeneity Results"


When I use this code it brings up the number Z as XX.X...X%
How do I set it so that it displays as XX.XX% with just the two decimal places?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try:

Code:
MsgBox "The Homogeneity of the Batch Model is:" & vbCrLf & vbCrLf & _
" " & Format(Z, "0.00%"), vbOKOnly, "Homogeneity Results"

Dom
 
Upvote 0
Try this:

Rich (BB code):
MsgBox "The Homogeneity of the Batch Model is:" & vbCrLf & vbCrLf & _
" " & Format(z, "0.00") & "%", vbOKOnly, "Homogeneity Results"

...you can also use Format(z,"0.00%") instead, and then your value would need to be a/b instead of (a/b*100).
 
Upvote 0
Is it?

MsgBox "The Homogeneity of the Batch Model is:" & vbCrLf & vbCrLf & _
" " & Format(Z, "0.00%"), vbOKOnly, "Homogeneity Results"
 
Upvote 0
This is exactly what I was looking for, I was just not sure how to write the code to do it.

For reference, I had to make it Z / 100 because it converted the number to a percentage assuming it was a decimal before (when I ran the code the number came out as XXXX.XX%)

So, what worked for me here was:

Format(Z / 100, ".00%")

My example had a number of 63.09487...%, which it showed as 63.09% which is what I was looking for.

Thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

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