Powerpoint VBA delete empty columns? - excel

I have several Powerpoint files being generated everymonth, these files have several charts with embedded excel tables behind them, for some month some series(columns) are full of zeros, I would like to find a Powerpoint Macro that goes into each chart looks on the embedded excel and deletes columns that are all zeros.
Now I have the code to do it in excel
Sub DeleteColumns()
Dim LR As Long, LC As Long
Dim i As Long, j As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious,SearchOrder:=xlByRows).Row
LC = 52
For j = LC To 1 Step -1
For i = LR To 1 Step -1
If Cells(i, j).Value = 0 Then
Columns(j).Delete
Exit For
End If
Next i
Next j
End Sub
And its working, I just dont know how to reference all the charts inside the powerpoint and then target the embedded excel tables behing them.
Many Thanks

You can expose all the underlying excel chart like so.
I'm not sure of how you are looking to remove the zero columns so suggest you add this in at the spot highlighted below.
Sub ChangeChartData()
Dim pptChart As Chart
Dim pptChartData As ChartData
Dim xlWorkbook As Object
Dim sld As Slide
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
Set pptChart = shp.Chart
Set pptChartData = pptChart.ChartData
pptChartData.Activate
Set pptWorkbook = pptChartData.Workbook
'your delete code here
pptWorkbook.Close True
End If
Next
Next
Set pptWorkbook = Nothing
Set pptChartData = Nothing
Set pptChart = Nothing
End Sub

Related

Change the data shown on a powerpoint chart

I have an excel sheet that has the data, I have a powerpoint presentation that has a few charts. I need to run a report eveyday, so i am trying to automate it. I wrote a vba script to copy and paste the data from excel sheet to the chart in powerpoint . But i am unable to change the selection region(the data that is displayed on the graph eventhough there may be more data).
I have written the following script. Any help that helps me change the data to be displayed on the chart is appreciated.
Private Sub CommandButton1_Click()
Dim r As Range
Dim powerpointapp As Object
Dim mypresentation As Object
Dim myslide As Object
Dim myshape As Object
Dim ppath As String
Dim titlesh As Object
Dim tdate As String
Dim chartsh As Object
tdate = Format(Date, "mmmm dd, yyyy")
ppath = "path to ppt"
Set powerpointapp = CreateObject(class:="PowerPoint.Application")
Set mypresentation = powerpointapp.Presentations.Open(ppath)
Set myslide = mypresentation.Slides(1)
Set titlesh = myslide.Shapes("Dateh")
titlesh.TextFrame.TextRange.Text = tdate
Set myslide = mypresentation.Slides(2)
Set chartsh = myslide.Shapes("Chart 6")
chartsh.Chart.ChartData.Workbook.Sheets(1).Cells.Clear
Set r = ThisWorkbook.Worksheets("Weekly Tracking").Range("B84:C158")
r.Copy
chartsh.Chart.ChartData.Workbook.Sheets(1).Range("A2:B74").Value = r.Value
powerpointapp.Visible = True
powerpointapp.Activate
Application.CutCopyMode = False
End Sub
Depends on what your data looks like. You may use .end or usedrange or a loop to looking for keyword in order to locate the end of data.
Some code just to demo the idea:
Dim lastline1 As Long, lastline2 As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Weekly Tracking")
lastline1 = ws.Cells(84, 2).End(xlDown).Row() 'Need to check whether row 84 is the last row, otherwise it may fly to 65536 or 1048576.
lastline2 = ws.UsedRange(ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count).Row
Set r = ThisWorkbook.Worksheets("Weekly Tracking").Range("B84:C" & lastline2)

Powerpoint to excel using Powerpoint VBA [duplicate]

I have this challenge to create a macro to extract data from ppt.
I need to extract the data from tables in a ppt and paste them into Excel.
I can extract data and paste it into Excel, but the tables are printing one below the other, like this:
I want the tables to to printed like this:
The below image is from ppt how the tables are placed in ppt,
in similar way the tables need to be printed in the Excel spreadsheet:
I tried this:
Sub ExportToExcelSheet()
'Declare PPT variables
Dim pptPres As Presentation
Dim pptSlide As Slide
Dim pptShape As Shape
Dim pptPlaceholder As PlaceholderFormat
Dim pptTable As Table
'Declare Excel variables
Dim xlApp As Excel. Application
Dim xlBook As Excel. Workbook
Dim xlSheet As Excel. Worksheet
Dim xlRange As Excel.Range
'Access the active presentation
Set pptPres = Application.ActivePresentation
On Error Resume Next
Set xlApp = GetObject(, "EXCEL.Application")
If Err.Number = 429 Then
Err.Clear
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
End If
Set xlBook = xlApp.Workbooks("Extract.xlsx")
Set xlSheet = xlBook.Worksheets("Sheet1")
For Each pptSlide In pptPres.Slides
For Each pptShape In pptSlide.Shapes
If pptShape.Type = msoTable Then
Set pptTable = pptShape.Table
pptShape.Copy
Set xlRange = xlSheet.Range("A100").End(xlUp)
If xlRange.Address <> "$A$1" Then
Set xlRange = xlRange.Offset(3, 0)
End If
xlSheet.Paste Destination:=xlRange
End If
Next
Next
xlSheet.Columns.Range("A1").ColumnWidth = 5
xlSheet.Columns.Range("B1").ColumnWidth = 25
xlSheet.Rows.RowHeight = 20
End Sub
Because xlRange is defined by searching the last used cell in Column A, you are only pasting to Column A and thus, your current output is one after the other down this column.
You can keep it this way and then reposition each table after they are in Excel by serching each title for "Sub*" and if found put it on row (say) 10 otherwise using a similar method to your last row, find the last column and offset it say, 3 columns to the right.
Something like this AFTER your existing for loop...
Dim RowCounter As Long
Dim BottomRowOfTable As Long
Dim LastColumn As Long
Dim MyArray As Variant
For RowCounter = 1 To xlRange 'You can reuse this as the last destination which would be the last header row in the list you pasted.
With xlSheet
If .Cells(RowCounter, 1).Value Like "Sub*" Then
BottomRowOfTable = .Cells(RowCounter, 1).End(xlDown).Row
LastColumn = .Cells(8, .Columns.Count).End(xlToLeft).Column
MyArray = .Range(.Cells(RowCounter, 1), .Cells(BottomRowOfTable, 1)).Value
.Range(.Cells(8, LastColumn + 2).Address).Resize(UBound(MyArray)).Value = MyArray
.Range(.Cells(RowCounter, 1), .Cells(BottomRowOfTable, 1)).Clear
ElseIf .Cells(RowCounter, 1).Value Like "Station*" Then
BottomRowOfTable = .Cells(RowCounter, 1).End(xlDown).Row
LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
MyArray = .Range(.Cells(RowCounter, 1), .Cells(BottomRowOfTable, 1)).Value
.Range(.Cells(1, LastColumn + 2).Address).Resize(UBound(MyArray)).Value = MyArray
.Range(.Cells(RowCounter, 1), .Cells(BottomRowOfTable, 1)).Clear
End If
End With
Next RowCounter
You will probably need to change some of the cell references so it targets the right cell to find the title and/or move each table to the correct location.
I also did not test with formatting per your current output to Excel.
I think there is probably a cleaner way to do this but this method can achieve your goal.

Is there a way to copy and paste multiple charts that are grouped in excel to powerpoint using VBA?

Is there a way such that i can copy and paste multiple charts that are grouped in four as shown below from excel to my existing powerpoint slides 28 and slides 29? The name of the groups are group 16 for the left group, group 17 for the right group. I have tried to use Chrt.CopyPicture but it only copies charts separately to the slides instead of a group like the one outline on the 4 charts shown on the left side of the picture below. By the way, my only code only copies charts individually to slide 28.
Sub ExportChartsTopptSingleWorksheet()
'Declare PowerPoint Variables
Dim PPTApp As Object
Dim PPTPres As Object
Dim PPTShape As Object
Dim mySlide, myslide2 As Object
'Declare Excel Variables
Dim Chrt As ChartObject
If PPTApp Is Nothing Then _
Set PPTApp = CreateObject(class:="PowerPoint.Application")
On Error GoTo 0
PPTApp.Visible = True
'Create new presentation in the PowerPoint application.
Set PPTPres = PPTApp.Presentations.Open(Filename:="\\fab2crp-nas1\home22\kkang2\Profile\Desktop\myassignment3\mypresentationsample.pptx")
Set mySlide = PPTPres.Slides.Add(28, 1)
'Loop through all the CHARTOBJECTS in the ACTIVESHEET.
For Each Chrt In ActiveSheet.ChartObjects
'Copy the Chart
Chrt.CopyPicture '<------ method copy fail error here
'paste all the chart on to exisitng ppt slide 28
mySlide.Shapes.Paste
Next Chrt
End Sub
Currently, charts are copied individually to ppt slides
Expected
This worked for me.
Sub ExportChartsTopptSingleWorksheet()
Const PER_ROW As Long = 2 'charts per row in PPT
Const T_START As Long = 40 'start chart top
Const L_START As Long = 40 'start chart left
Dim PPTApp As Object
Dim PPTPres As Object
Dim PPTShape As Object
Dim mySlide, myslide2 As Object, i As Long
Dim Chrt As ChartObject, T As Long, L As Long
If PPTApp Is Nothing Then _
Set PPTApp = CreateObject(class:="PowerPoint.Application")
PPTApp.Visible = True
Set PPTPres = PPTApp.Presentations.Add()
Set mySlide = PPTPres.Slides.Add(1, 1)
i = 0
For Each Chrt In ActiveSheet.ChartObjects
Chrt.Chart.CopyPicture
i = i + 1
'work out the top/left values
T = T_START + (Application.Floor((i - 1) / PER_ROW, 1)) * Chrt.Height
L = L_START + ((i - 1) Mod PER_ROW) * Chrt.Width
With mySlide.Shapes
.Paste
.Item(.Count).Top = T
.Item(.Count).Left = L
End With
Next Chrt
End Sub

VBA Copy/Paste Issues - PPTX Creation

Im using the below code to iterate over a table in excel that contains named ranges and position details for cells id like to copy over to a powerpoint presentation.
The code works perfectly. Except that, and for some reason its always random, the code throws a "Shapes.paste invalid request clipboard is empty" error. Debugging didnt help since it always stops at a different object or named range. I know VBA is a little finicky with its operations in that it starts the paste before actually completing the copy operation.
I tried the Application.Wait function which isnt the best solution, it slowed the code by 3 fold. As well do/doevents calls didnt help.
Any ideas on how to curb this VBA issue ??
Thanks!
Sub MyProcedure(PPT As Object, WKSHEET As String, RangeTitle As Range, SlideNumber As Long, FTsize As Variant, FT As Variant, SetLeft As Variant, SetTop As Variant, SetHeight As Variant, SetWidth As Variant, Bool As Boolean)
Dim shP As Object
Dim myShape As Object
Dim mySlide As Object
Dim tempSize As Integer, tempFont As String
Dim Mypath As String
Dim Myname As String
Dim myTitle As String
Dim ws As Worksheet
'Application.Calculation = xlManual
'Application.ScreenUpdating = False
Set ws = Worksheets(WKSHEET)
'select the name of report
Set shP = ws.Range(RangeTitle)
'select the ppt sheet you wish to copy the object to
Set mySlide = PPT.ActivePresentation.slides(SlideNumber)
'count the number of shapes currently on the PPT
shapeCount = mySlide.Shapes.Count
'copy the previously selected shape
Do
shP.Copy
'paste it on the PPT
DoEvents
mySlide.Shapes.Paste 'Special DataType:=ppPasteOLEObject, Link:=msoFalse
'wait until the count of shapes on the PPT increases, which signals that the past operation is finished.
'<~~ wait completion of paste operation
Loop Until mySlide.Shapes.Count > shapeCount
'adjust formatting of the newly copied shape: position on the sheet, font & size
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
With myShape
.Left = SetLeft
.Top = SetTop
.Width = SetWidth
.Height = SetHeight
.TextEffect.FontSize = FTsize
.TextEffect.FontName = FT
.TextEffect.FontBold = Bool
End With
'Application.CutCopyMode = False
'Application.Calculation = xlAutomatic
'Application.ScreenUpdating = True
End Sub
Sub LoopThrougMyData()
Dim FirstRow As Integer: FirstRow = 1
Dim LastRow As Integer: LastRow = Worksheets("Table").Range("A1").End(xlDown).Row
Dim iRow As Long
Dim PPT As Object
Set PPT = CreateObject("PowerPoint.Application")
Myname = ThisWorkbook.Name
Mypath = ThisWorkbook.Path
PPT.Visible = True
PPT.Presentations.Open Filename:=Mypath & "\Actuals Review Temp.pptx"
For iRow = FirstRow To LastRow 'loop through your table here
With Worksheets("Table").Range("test")
MyProcedure PPT, WKSHEET:=.Cells(iRow, "A"), RangeTitle:=.Cells(iRow, "B"), SlideNumber:=.Cells(iRow, "C"), FTsize:=.Cells(iRow, "D"), FT:=.Cells(iRow, "E"), SetLeft:=.Cells(iRow, "F"), SetTop:=.Cells(iRow, "G"), SetHeight:=.Cells(iRow, "H"), SetWidth:=.Cells(iRow, "I"), Bool:=.Cells(iRow, "J")
'call the procedure with the data from your table
End With
Next iRow
End Sub
It's more than likely a clipboard issue. This is a common bug in VBA when copying information from one application to the other application. The best solution I've found so far is simply pausing the Excel application for a few seconds in between the copy and paste. Now this won't fix the issue in every single instance but I would say 95% of the time it fixes the error. The other 5% of the time is simply the information being removed from the clipboard randomly.
Change this section:
shP.Copy
'paste it on the PPT
DoEvents
mySlide.Shapes.Paste 'Special DataType:=ppPasteOLEObject, Link:=msoFalse
With this:
'Copy the shape
shP.Copy
'Pause the Excel Application For Two Seconds
Application.Wait Now() + #12:00:02 AM#
'Paste the object on the slide as an OLEObject
mySlide.Shapes.Paste 'Special DataType:=ppPasteOLEObject, Link:=msoFalse

Paste named range from Excel to Powerpoint into named shape - VBA

I want to build a macro that connects our Excel-Data-Sheet with our Reporting-Powerpoint-Presentation.
So I have this named Range ("A") selected and copied.
Then I want to paste the data into a shape in Powerpoint which has the same name as my Range ("A").
Sub SyncWithPPT()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptShape As PowerPoint.Shape
Set pptApp = New PowerPoint.Application
pptApp.Visible = msoTrue
Set pptPres = pptApp.presentations.Open("workingPath")
ActiveWorkbook.Names("A").RefersToRange.Select
Selection.Copy
Set pptShape = pptPres.Slides("anySlide").Shapes("A")
pptShape.Table.cell(1, 1).Shape.TextFrame.TextRange.Paste 'Here it won't paste correctly
End Sub
Everything works just fine, except the pasting. When I paste the selection everything is pasted into cell(1, 1).But I want to copy each cell into a different cell. Like it does when you paste with STRG + V.
Any help would be really appreciated.
This worked for me (Office 2007)...
Sub Tester()
Dim ppt, sld
'presentation is already open...
Set ppt = GetObject(, "powerpoint.application")
Set sld = ppt.activepresentation.slides(1)
ActiveSheet.Range("A1:B2").Copy
sld.Shapes(1).Table.Cell(1, 1).Select
ppt.ActiveWindow.View.Paste
Set sld = Nothing
Set ppt = Nothing
End Sub
'this is how to extract each cell information
'assuming that ppt communication is already done.
Dim n As Integer, j As Integer
Dim ultimaFila As Long
j = 1 'columna
ultimaFila = Range("A65536").End(xlUp).Row
For n = 1 To ultimaFila
pptShape.Table.cell(n, j).Value = Application.Workbooks("Book1").Worksheets("Sheet1").Cells(n, j).Value
Next n
End Sub

Resources