I have created a macro that creates tables, a power point and then copies the tables and pastes them on the slides. But sometimes, the code skips the line where I copy this table (table.copy). I can't find any regularity in these lines-skipping. When I write this line several times, my program runs perfectly. Otherwise, it sometimes stops at the line where it it supposed to paste the table and says "The specified data type is unavailable". Then I replace the cursor on the previous line ("copy") and it works... until the next time the same happens. If any one has an idea thanks a lot !
Sub CreatePPT()
'Declare the variables
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim oldProduct As String
Dim Product As String
Dim MN As String 'month number
Dim Year As String
Dim Cluster As String
Dim i As Integer
Dim KPIindex As Integer
Dim table As Range
'actualisation oldProduct (to be replaced in KPI table)
oldProduct = ActiveWorkbook.Worksheets(3).Cells(28, 14)
'Select Global Slicers
Cluster = InputBox("Cluster")
MN = InputBox("Please enter month number (ex 05)")
Year = InputBox("Please enter year (ex 2018)")
KPIindex = slicerCountry(Cluster)
slicerDate MN, Year
'Create a new PowerPoint
Set newPowerPoint = New PowerPoint.Application
'Make a presentation in PowerPoint
newPowerPoint.Presentations.Add
'Loop on the products
For i = 1 To 6
'Change slicer and actualisation order type
Product = slicerProduct(i)
If i > 1 Then 'close former KPI file
Name = oldProduct & " KPI.xlsx"
Workbooks(Name).Close (False)
End If
'Open current KPI file, then reactivate working file
Filename = "C:\Users\moi\Documents\" & Product & " KPI.xlsx"
Workbooks.Open (Filename)
Windows("charlotte.xlsm").Activate
'actualisation of the europe global KPI table according to the product
Application.Goto Reference:="KPI"
Selection.Replace What:=oldProduct, Replacement:=Product, LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
oldProduct = Product
ActiveWorkbook.Worksheets(3).Cells(28, 14) = oldProduct
'Set up KPI local table with the datas imported on KPIs sheet from the corresponding KPI file
ActiveWorkbook.Worksheets(1).Cells(63, 21) = ActiveWorkbook.Worksheets("KPIs").Cells(18, KPIindex)
ActiveWorkbook.Worksheets(1).Cells(64, 21) = ActiveWorkbook.Worksheets("KPIs").Cells(19, KPIindex)
ActiveWorkbook.Worksheets(1).Cells(68, 21) = ActiveWorkbook.Worksheets("KPIs").Cells(24, KPIindex)
ActiveWorkbook.Worksheets(1).Cells(69, 21) = ActiveWorkbook.Worksheets("KPIs").Cells(25, KPIindex)
ActiveWorkbook.Worksheets(1).Cells(73, 21) = ActiveWorkbook.Worksheets("KPIs").Cells(29, KPIindex)
ActiveWorkbook.Worksheets(1).Cells(74, 21) = ActiveWorkbook.Worksheets("KPIs").Cells(30, KPIindex)
ActiveWorkbook.Worksheets(1).Cells(75, 21) = ActiveWorkbook.Worksheets("KPIs").Cells(31, KPIindex)
'Add a new slide for the orders related to the current product (charts & tables & title & comments)
newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
activeSlide.Shapes(2).TextFrame.TextRange.Text = Product & " - Orders"
activeSlide.Shapes(1).TextFrame.TextRange.Text = "Comments"
'Copy the table of top five orders and paste it into the PowerPoint as a Metafile Picture
Set table = Range("top_five")
table.Copy
activeSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
'Adjust the positioning of the table on Powerpoint Slide
activeSlide.Shapes(3).Width = 263
activeSlide.Shapes(3).Left = 230
activeSlide.Shapes(3).Top = 270
'Copy the table of HTD Orders and paste it into the PowerPoint as a Metafile Picture
Set table = Range("growth")
table.Copy
activeSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
'Adjust the positioning of the table on Powerpoint Slide
activeSlide.Shapes(4).Width = 261
activeSlide.Shapes(4).Left = 230
activeSlide.Shapes(4).Top = 70
'Copy the table of KPI and paste it into the PowerPoint as a Metafile Picture
Set table = Range("ClusterKPI")
table.Copy
activeSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
'Adjust the positioning of the table on Powerpoint Slide
activeSlide.Shapes(5).Width = 200
activeSlide.Shapes(5).Left = 20
activeSlide.Shapes(5).Top = 96
Next
'close the last KPI file opened
Name = oldProduct & " KPI June.xlsx"
Workbooks(Name).Close (False)
AppActivate ("Microsoft PowerPoint")
Set activeSlide = Nothing
Set newPowerPoint = Nothing
End Sub
I have seen it had been a solution for someone else, but I already verified in my macro settings and "Trust programmatic access to VBA object model" is turned on...
Since this is definitely no proper solution to just copy-paste ten times in a row the same code line hoping one of them won't be skipped, if someone could help me using the "On Error GoTo" tool it would also be a great help, because I tried to write
Set table = Range("ClusterKPI")
table.Copy
On Error GoTo 135 'where 135 is the number of the previous line
activeSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
But it got a Compile error : label not defined.
Thanks again
Excel copies and pastes the data so fast that it needs some time to switch the application.
Try adding the below code before pasting the values
Application.Wait(Now + TimeValue("0:00:02")) '2 represents 2 seconds