Changing the format of numbers using the Excel object in VB

Posted on 2000-04-03
Last Modified: 2011-09-20
How can I change the decimal point of numbers in an Excel spreadsheet through VB? The code I'm using is this:

.Range("B6:I16").Value.NumberFormat = "#0.00"

which only changes the display of numbers but not the value, which is still in it's original form.
Question by:wsteph

Author Comment

ID: 2680567
I used the wrong code fragment, it should be:

..Range("B6:I16").NumberFormat = "#0.00"
LVL 17

Expert Comment

ID: 2680647
Hi WSteph,

If you need to round the numbers as displayed by your previous command, use this little code part (preceded by .)

For Each Cell In .Range("B6:I16")
Cell.Value = Application.WorksheetFunction.Round(Cell, 2)
Next Cell

The 'Application' is the running excel instance in this example, replace it by what you have as an Excel application if you're operating from outside.

Hope this helps


Accepted Solution

riduce earned 50 total points
ID: 2680997
Here is you're answer
add this to you're code:

ActiveWorkbook.PrecisionAsDisplayed = True

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now