0

I am new to this forum, and new to VBA. I am working with a very large Excel table (200+ rows) where I have added a code in to identify page breaks based on when a value changes in one of the columns. I've figured out how to paste my range of data into a PowerPoint slide, but I need it to paste onto separate slides according to where/when the page break is set.

Here is my page break code:

J = ActiveSheet.Cells(Rows.Count, "I").End(xlUp).Row
For I = J To 2 Step -1
    If Range("I" & I).Value <> Range("I" & I - 1).Value Then
        ActiveSheet.HPageBreaks.Add Before:=Range("I" & I)
    End If
Next I

And here is what I have so far to get range into PowerPoint, but this pastes everything on one slide:

Dim rng As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object

'Copy Range from Excel
Set rng = ThisWorkbook.ActiveSheet.Range("A1:J5")

'Create an Instance of PowerPoint
  On Error Resume Next

'Is PowerPoint already opened?
  Set PowerPointApp = GetObject(class:="PowerPoint.Application")

'Clear the error between errors
  Err.Clear

'If PowerPoint is not already open then open PowerPoint
  If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")

'Handle if the PowerPoint Application is not found
  If Err.Number = 429 Then
    MsgBox "PowerPoint could not be found, aborting."
    Exit Sub
  End If

  On Error GoTo 0

'Optimize Code
  Application.ScreenUpdating = False

'Create a New Presentation
  Set myPresentation = PowerPointApp.Presentations.Add

'Add a slide to the Presentation
  Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly

'Copy Excel Range
  rng.Copy

'Paste to PowerPoint and position
  mySlide.Shapes.PasteSpecial DataType:=2  '2 = ppPasteEnhancedMetafile



Set shp = PowerPointApp.ActiveWindow.Selection.ShapeRange

'Center Object
  With myPresentation.PageSetup
    shp.Left = (.SlideWidth \ 2) - (shp.Width \ 2)
    shp.Top = (.SlideHeight \ 2) - (shp.Height \ 2)


'Make PowerPoint Visible and Active
  PowerPointApp.Visible = True
  PowerPointApp.Activate

'Clear The Clipboard
  Application.CutCopyMode = False

  End With
3
  • First, rearrange the second chunk of code so that Set rng and set myslide and rng.copy are together, so you can loop the range-setting and copying. Then: I don't know anything about page breaks, but it seems clear that your two options here are: 1. Keep your two-sub structure; research page breaks and how you can get the code to detect one, and loop the range-set-and-copy every time you encounter a page break; 2. Dump the page break thing and incorporate the for each row loop in the first sub into the loop in the main sub using the range().value <> range(,-1).value setup.
    – Alex M
    Commented Feb 20, 2020 at 17:38
  • @AlexM Thanks for responding! The first code works in excel to detect page breaks based on when a value changes, i just don't know how to incorporate that into the second (bigger) code so that i can tell it to loop through the page breaks?? I can't set a certain number of rows for it to then go to a new slide, because the text in the rows are different each time aka different row heights.
    – alyssakptn
    Commented Feb 21, 2020 at 14:01
  • I know, the code provided below to loop every n th row is mostly useless to you, don't worry about that. "The first code works in excel to detect page breaks based on when a value changes" This is not correct - the first code inserts page breaks based on when a value changes. If using that code, what you need is to get the second code to detect a page break, but I don't know how to do that (or if it's possible - I haven't looked into it at all - I assume you have done or are doing that, as I suggested in my point 1).
    – Alex M
    Commented Feb 21, 2020 at 19:50

1 Answer 1

-1

You can use this VBA code, takes every 15 row (as page) to paste into new slide.

Private Sub Multipage_Slide()

Dim LastRow as Long, i as Long, j as Integer, _   

rngH as Range, wss as Worksheet

LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

Set rngH = ws.Range("A1:L1") 
i = 2

Set wss = wb.Worksheets.Add

Do While i <= LastRow
    j = Application.Min(i + 13, LastRow)
    Union(rngH, ws.Range("A" & i, ws.Range("L" & j))).Copy Destination:= wss.Range("A1")

    Set sld = slds.Add(myPres.Slides.Count + 1, ppLayoutBlank)
    wss.Range("A1:L" & j-i+2).Copy

    sld.Shapes.PasteSpecial DataType:=0
    sld.Shapes(1).Top = 100
    sld.Shapes(1).Left = 100


    Set pptextbox = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 22, 60, 700, 60)

    With pptextbox.TextFrame
        .TextRange.Text = "Multiple Slides"  
        .TextRange.Font.Bold = msoTrue
        .TextRange.Font.Name = "Arial(Headings)"
        .TextRange.Font.Size = 20
        .TextRange.Font.Color.RGB = RGB(0, 51, 102)
    End With
    i = j + 1
Loop

Application.DisplayAlerts = False
wss.Delete
Application.DisplayAlerts = True
Set wss = Nothing

End Sub

N.B.

  • A1:L1 is Header, you may adjust it.
  • Last Row value Application.Min(i + 13, LastRow), can be adjusted (currently picks 15 Row).
  • Font name, color & size also are editable.
4
  • @AlexM,, thanks for observation,,, but could you please ,, write your concern and the mistake in this code !! Commented Feb 21, 2020 at 5:41
  • I wrote my concern in my comment (which was evidently deleted - odd, that; I thought we were supposed to write comments justifying downvotes). This answer doesn't address the question. Q: How do I get this VBA that pastes a range into PP to recognize page breaks as signal to paste onto a new slide? A: Here's contextless code that pastes every 15 lines onto a new slide. It's just not relevant. You could have removed everything except the 2nd nota bene and it would have been exactly as helpful (ie almost not at all).
    – Alex M
    Commented Feb 21, 2020 at 19:47
  • @AlexM,,, if U read fine lines by OP I hope you realize that OP's need is to Paste every 15 rows in new slide and my code is doing same !! Commented Feb 22, 2020 at 4:07
  • I think you must be thinking of a different question. There's nothing like that in this question.
    – Alex M
    Commented Feb 25, 2020 at 0:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .