PowerPoint VBA - extending chart data range - vba

I am trying to create a VBA macro in PowerPoint that will extend the range of an excel embedded chart to the last row with data. How can this be achieved?

You can use "lastrow," which is expressed as an integer.
Table.LastRow
So, depending on your slide, you might say something like
ActivePresentation.Slides(3).Shapes(5).Table.lastRow
to get the last row number.

Related

Vba email generator, subject from excel spreadsheet

I have a vba code that generates an email. I would like the subject to be the data from the first and last cells in my list. The thing is, my list isnt of a set length, sometimes it contains 5 pieces of data sometimes 8 etc. How do i tell vba to pick the first and last cell no matter the length of the list?
thanks
For me, best practice is to just have cells on your sheet that calculate the first and last row (different ways you can do that), then give those cells a range name such as FirstRow and LastRow. In your vba then you refer to these cells to make your code dynamic.
e.g:
firstRow = Range("FirstRow)
lastRow = Range("lastRow")
test = range(cells(firstRow,lastRow))
-- Note I have not written VBA in many many years so am writing the above from memory so it may be not be exact.
Of course you can do it all entirely in VBA using the xlDown method mentioned previously but I prefer the transparency of it being on the main page so that easily spot if something breaks.
Range("A1").End(xlDown).Value
Where the cell is where you want to start and the End part moves all the way to the end

Running an IF statement for all worksheets

I have data arranged in Excel like so:
Year|Trade Flow|Commodity Code|Commodity|Qty Unit|Qty|Net Weight|Trade Value
I have an IF formula:
=IF(D2=D1,0,IF(D3=D2,ABS(I3-I2),I2))
which I need to run for all the worksheets in the file, using VBA.
I am assuming you know how to find the last cell (plenty of examples on this site), or have a fixed size.
I used column H just for an example.
for each sh in sheets
sh.range("H2:H20").Formula="=IF(D2=D1,0,IF(D3=D2,ABS(I3-I2),I2))"
next
alter this to suit your needs

Flexible Data Label Formatting

I'm hoping to add custom data labels to a stacked bar chart in Excel 2010. Each bar will have a label value pulled from a value in the sheet which is not necessarily equal to the height of the bar itself.
The sheet I'm making is a template and has drop-down selections to change the units of the data. For example: $MM with 1 decimal point, K with 2 decimal points, $ with 0 decimal points, etc... I'm running into an issue formatting the labels to match the data formatting selections. I've tried two approaches:
Formatting the data labels themselves using VBA. Partial code below for $K with one decimal:
`With ActiveChart.SeriesCollection(x).Points(y)`
.DataLabel.Text.NumberFormat = "$#,##0.0,"
Unfortunately, the labels don't seem to read the commas and divide by one thousand, so I can't accurate display thousands or millions. A value of 1000 in the above code will display as $1,000.0 instead of the desired $1.0.
Format the values in the worksheet, and then just read them with VBA. Basically, I use conditional formatting to properly display the values on the sheet, and then read them with .DataLabel.Text = Cells(r,c)
Unfortunately, though the data is formatted correctly, the values are unchanged and the labels display with no custom formatting at all.
Is there any reasonable way to achieve my goal? I'm hoping there's a way to either (1) make commas count when changing data label number formats or (2) change the actual values of the data in my sheet based on the formatting drop-down selections (instead of just giving them custom formatting which leaves the value unchanged).
Thank you,
Lance
I think the words "conditional formatting" are your issue here: I've had problems with VBA reading the underlying format of a cell when it's conditionally formatted (admittedly, my issue was with colour, rather than number format, but I expect the reasons for it not working would be the same).
If you adopt your second approach, i.e. relying on the worksheet's format using the chart's "Linked to source" option, then on a trigger of when the drop-down option is changed, you could use VBA to set the format on the worksheet instead of using conditional formatting - then the chart would pick up whatever format you're using.
Please try this:
?Format(1000,"$#,##0.0,")
$1.000,0
?Format(1000,"$#,##0,.0")
$1,0
?Format(1234,"$#,##0,.0")
$1,2

format numbers in text box in ppt via vba

I have connected Excel and Powerpoint via VBA to send values from the Excel sheet to the PPT.
All is working well except one thing: I need to transfer values from cells in Excel to text box shapes in ppt while preserving the number formatting from excel. How do I do that?
I do this for about 10 such boxes and my current code using copy from excel and paste in powerpoint, keeps on giving out of range error on random places.
Will paste the code I am using in a short while.
Try using the numberFormat from Excel when you bring over the Value.
Example:
With Workbooks(1).Sheets(1).Range("A1")
valueToPaste = Format(.Value, .NumberFormat)
End With
For the sake of the example, I'm pretending you are calling this from Excel and only want to know how to extract the value with it's format. We are using the first sheet of the first workbook in Range A1. It should be easy enough to update to your specific needs.
There are probably some exceptions, particularly for custom formats, but this should work for the majority of formats you would use in Excel.

Problem using SUM(If( Rather than SumIf(

I have written a Formula in Excel that Sums up Items in 1 Column (Text) the Column is a Dynamic Named Range (Invoice_list_Item) the Formula sets a Value (Of my Choice) based on each Item in the Range and Sums those Values, I am now using a Userform where I would like to Display that Equation rather than in the workbook itself, so my problem is that everytime I write this code in the userform it doesn't work; either throwing an error, or not displaying a value. I have tried Application.Sum, Application.Sumif, Application.Sumproduct and the main one I've been trying to make work Application.Sum(If(Range(
Any Help in correcting this Formula in to a Macro will be much appreciated.
=SUM(IF(Invoice_list_Item="1/4 Item",0.25,0)+IF(Invoice_list_Item="1/2 Item",0.5,0)+IF(Invoice_list_Item="1 Item",1,0))
You can use Application.Evaluate (this works for array formulae) to evaluate a string as if it was an Excel formula:
Dim vResult As Variant
vResult = Application.Evaluate("=SUM(IF(Invoice_List_Item=""1/4 Item"",0.25,0)+IF(Invoice_List_Item=""1/2 Item"",0.5,0)+IF(Invoice_List_Item=""1 Item"",1,0))")