VBA scientific format of number...

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have a number: -300.565181720644

I need to output from my VBA code as 'scientific' format (##0.0E+0)

I've found that if I format a cell to ##0.0E+0, the vba outputs to the sheet: -300.6E+0 which is correct

but if I try and format the number within VBA (and not save to cell in sheet) using:

Code:
Format(-300.565181720644, "Scientific")

I get just -300

or if I use

Code:
Format(-300.565181720644, "##0.0E+0")

I get -300.6

How can I get this to output from VBA to get the result of -300.6E+0 ?

If you can point me in the right direction, I'd be very grateful

Thanks

Rob
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You could use a custom format such as:

Code:
With Range("A1")
    .Value = -300.565181720644
    .NumberFormat = "##0.0E+0"
End With
 
Upvote 0
Thanks for that Steve. This solution does work, but I'm looking to render the value as 'TEXT' so I need the VBA to output as the finished value ie. -300.6E+0

No matter what I do, it always outputs as the full value ie.
-300.565181720644 and it's only the cell formatting that puts it into the correct format.

I need the scientific value outputted as text directly from VBA...

Is this possible?...
 
Upvote 0
That isnt a true scientific number so do you want the true one or your custom one?
 
Upvote 0
For my purposes, I'm not really interested in the number as it's not going to be used for any further calculations.

If I can convert the characters in the cell (-300.6E+0) to 'text' so that it doesn't convert back to the full number, it'll solve the problem

I'll have a play to see if I can use your cell formatting idea to output my final value.

Thanks Steve
 
Upvote 0
Excel is going to keep trying to convert that text to a number if it can when you input it. You can either format the input cell to text before you enter the value or maybe use this:

Code:
Range("A1").Value = "'" & Format(-300.565181720644, "scientific")
 
Upvote 0
OOoooh, Steve, you are a legend... I can pull that out as text now!!!!

Out of interest, what does the apostrophe do in the cell?
 
Upvote 0
Just tells excel you want to use text. If you input '1 into a cell that number will be text not a number.
 
Upvote 0
Brilliant. Thanks for your help Steve. That's a useful thing to know!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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