Reputation: 137
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?
Upvotes: 0
Views: 1336
Reputation: 4367
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 A1
contains the value 1.234
. Using this code:
ActiveSheet.Range("A1").NumberFormat = "0.0"
ActiveSheet.Range("A2").Value = ActiveSheet.Range("A1").Value
ActiveSheet.Range("A3").Value = ActiveSheet.Range("A1").Text
The result is like so:
A1
contains the value 1.234
, but displays as 1.2
A2
contains the value 1.234
, and displays as 1.234
A3
contains the value 1.2
, and displays as 1.2
The .Text
property 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:
Dim s As String
Dim dec As Integer
dec = 2
s = CStr(ActiveSheet.Range("A1").Value)
ActiveSheet.Range("A3").Value = CDbl(Left(s, InStr(s, ".")) & Left(Right(s, Len(s) - InStr(s, ".")), dec))
In the above, you can extend the number of decimal places by increasing the value of dec
. However, without further modification 1.000
will come out as 1
(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).
Upvotes: 1