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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 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