number variable format in VBA

DRaffo

New Member
Joined
Jul 12, 2007
Messages
6
Is there any way to format a number variable in VBA, maybe when declaring it, so that it is displayed with the 3rd number coma and 2 decimal places? ie. 3,556.98
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Code:
Sub test2()
Dim x, y As String
x = 3556.98
y = Format(x, "#,000.00")
MsgBox y
End Sub
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,023
Office Version
  1. 365
Platform
  1. Windows
How are you displaying it? If is a Message Box, do something like this:
Code:
    Dim myValue As Double
    myValue = 3556.98
    
    MsgBox Format(myValue, "#,##0.00")
 

DRaffo

New Member
Joined
Jul 12, 2007
Messages
6
thanks for the quick reply!

both gave me what i needed.

its use was to later be displayed in excel in the middle of a string:

Code:
pounds = tons * 2204.62262

pounds2 = Format(pounds, "#,000.00")

Cells(40, 2) = "Total weight is " & pounds2 & " pounds"
 

Watch MrExcel Video

Forum statistics

Threads
1,130,186
Messages
5,640,688
Members
417,161
Latest member
Devon150

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
Top