Hi,
What I am trying to do is to write a VBA code within powerpoint that can do the following thing:
1. Add a new Slide and Insert a Chart
2. Open the chart data of this chart
3. Open a csv file, the path of which will be passed to it, and copy the content. Now this is not a 'copy all content as it is'. The CSV file will have data in this format.
SubSection | <Header> | |
Variables | <Segment1> | <Segment2> |
Var1 | 48.072 | 16.382 |
Var2 | 30.441 | 18.316 |
Var3 | 15.565 | 19.976 |
Var4 | 5.51 | 21.087 |
Var5 | 0.413 | 21.65 |
The first row shows the header that should be the title of the active slide.
The variables Var 1 - Var 5 should show up on the X Axis and the segments Segment1 - Segment 2 should show up as adjacent bars in the chart. For eg. The chart for the above data will look like this.
4. This data will be appropriately copied in the chart data and the title will also be changed.
I have created a function that takes in two parameters - path - path of the excel file (CSV) index - As there will be an array of file paths that will be passed to the function one by one in a loop, index is the counter of the loop, and is there just in case I need it for something.
I have commented function calls to functions or subs created by me explaining what they do.
Please find the code below.
Problem:
The problem I am facing is the data file can have multiple variables and multiple segments, and the code was made to incorporate that, however sometimes the chart only includes 3 variables and not more than that, and sometimes even though it has only one variable within the chart data but the chart shows empty blanks for 2 more. I have tried 2 days to figure out if it was something logical or Microsoft specific, but I could not. Please help. I really need to fix this asap.
Please find the code below:
Sub CreateChart(ByVal path As String, ByVal index As Integer) Dim myChart As Chart Dim gChartData As ChartData Dim gWorkBook As Excel.Workbook Dim gWorkSheet As Excel.Worksheet Dim oLayout As CustomLayout Dim oSl As Slide 'CREATING THE NEW SLIDE AND ADDING A CHART TO IT AND SETTING THE CHART DATA OBJECT Set oLayout = ActivePresentation.Slides(1).CustomLayout Set oSl = ActivePresentation.Slides.AddSlide(ActivePresentation.Slides.Count, oLayout) Set myChart = oSl.Shapes.AddChart.Chart Set gChartData = myChart.ChartData 'OPENING THE EXCEL FILE SPECIFIED BY THE path PARAMETER path = path Dim xlApp As Excel.Application Set xlApp = New Excel.Application xlApp.Visible = True Dim xlWBook As Excel.Workbook Set xlWBook = xlApp.Workbooks.Open(path, True, False) xlWBook.Worksheets(1).Activate Dim titleText As String 'COUNTING THE NUMBER OF ROWS AND COLUMNS THAT HAS TO BE COPIED LR = Range("A" & Rows.Count).End(xlUp).Row lColumn = xlWBook.ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column titleText = xlWBook.ActiveSheet.Cells(1, 2) 'SETTING THE TITLE TEXT VARIABLE 'GETTING THE DATA IN AN ARRAY ReDim dataArray(1 To (LR - 1), 1 To lColumn) As Variant For r = 1 To (LR - 1) For c = 1 To lColumn dataArray(r, c) = xlWBook.ActiveSheet.Cells(r + 1, c) Next c Next r 'WORKING WITH CHART DATA Set gWorkBook = gChartData.Workbook Set gWorkSheet = gWorkBook.Worksheets(1) gWorkSheet.Activate gWorkSheet.ListObjects("Table1").Resize gWorkSheet.Range("A1:B2") 'THIS WAS ADDED TO RESET THE TABLE SIZE TO 2 CELLS myChart.Refresh gWorkSheet.ListObjects("Table1").Resize gWorkSheet.Range("A1:" & getColLetter(lColumn) & (LR - 1)) 'SETTING THE TABLE SIZE TO THE DATA (getColLetter IS A CUSTOM FUNCTION TO RETURN COLUMN RETURN WHICH WORKS FINE) myChart.Refresh For r = 1 To (LR - 1) For c = 1 To lColumn gWorkSheet.Range(getColLetter(c) & r).Value = dataArray(r, c) Next c Next r gWorkSheet.ListObjects("Table1").Resize gWorkSheet.Range("A1:" & getColLetter(lColumn) & (LR - 1)) 'SETTING THE TABLE SIZE AGAIN, JUST TO BE SURE myChart.Refresh 'I AM DOING A LOT OF REFRESHES JUST TO BE SURE myChart.Refresh 'SETTING THE TITLE TEXT OF THE SLIDE ActivePresentation.Slides(ActivePresentation.Slides.Count - 1).Shapes.Placeholders(1).TextFrame.TextRange.Text = titleText gWorkBook.Application.Quit Set gWorkSheet = Nothing Erase dataArray LR = 0 lColumn = 0 Set gWorkBook = Nothing Set gChartData = Nothing Set myChart = Nothing xlApp.Application.Quit Set xlApp = Nothing End Sub
Any help appreciated.
Thanks in advance.