I have this code that aggregates multiple workbooks into a preview file where all of the data is clearly laid out in tables and charts. Then those tables and charts are pasted in a PowerPoint presentation.
I am having issues getting the right formatting to work when the values in the tables are copied over to the PowerPoint slides. One of the source worksheets outputs values like 847389.240398230423, and when you try to put that in PowerPoint, you get a value that takes up too many lines and looks like 2.240398230423E-02. That kind of formatting does not look great in any meeting.
I wrote some code that will edit the code while the values are in the Preview workbook
Sheets("Output").Select
'Select1
Range("C10:H15").Select
Selection.NumberFormat = "0.000"
'Select2
Range("C17:H17").Select
Selection.NumberFormat = "0.000"
'Select3
Range("C18:H19").Select
Selection.NumberFormat = "0.00"
'Select4
Range("C20:H21").Select
Selection.NumberFormat = "0.00"
This code works perfectly for the data that I need and shows up like 0.487, but the actual values in the cells are stuff like 0.487682742490, and I think that is what the code is recognizing when it comes time to update the PowerPoint presentation.
Is there a way that you can write some code that will only take the first 2 or 3 digits after the decimal place and ignore the other non-significant figures?
There are a few ways to achieve this. One way is to force the formatted/visible data as the actual value.
For example, let’s say cell
A1contains the value1.234. Using this code:The result is like so:
A1contains the value1.234, but displays as1.2A2contains the value1.234, and displays as1.234A3contains the value1.2, and displays as1.2The
.Textproperty of a cell is essentially what you see in the cell. Once you convert your values in this way, transferring to PP should work as intended.Alternatively (though not preferred because it is somewhat more cumbersome to use and requires strings, rather than just sticking to values) you can use something like this:
In the above, you can extend the number of decimal places by increasing the value of
dec. However, without further modification1.000will come out as1(removes possibly significant 0’s).If you use this method, you can either retain a numeric value (using
CDbl()) or keep it as a string if you do not need to perform any arithmetic (i.e. just put into PP).