FOR EACH WS starts at the very last sheet. Why?

VBAWannabee2

New Member
Joined
Feb 23, 2022
Messages
28
Office Version
  1. 2010
Platform
  1. Windows
Hi all!

My concern is what exactly the thread title is.
This is the code that I used to copy excel data then paste it on PPT but it starts the loop at the very last sheet.
Why is that? Appreciate the help in advance!

VBA Code:
Sub CreateNewPres()

    Dim ppt As PowerPoint.Application
    Dim pres As PowerPoint.Presentation
    Dim sh As PowerPoint.ShapeRange
    Dim sl As PowerPoint.Slide
    Dim ws As Worksheet
    
    Set ppt = GetObject(Class:="Powerpoint.Application")
    Set pres = ppt.Presentations("TESTFILE - Copy.pptx")

For Each ws In ActiveWorkbook.Worksheets

If ws.Visible = True Then

    Set sl = pres.Slides.Add(3, ppLayoutBlank)

    ws.Range("A1:D8").Copy

    Set sh = sl.Shapes.PasteSpecial(ppPasteEnhancedMetafile)
    
    'sh.Top = 66
    'sh.Left = 152
    sh.LockAspectRatio = msoFalse
    sh.ScaleHeight 1.24, msoTrue, msoScaleFromMiddle
    sh.ScaleWidth 1.33, msoTrue, msoScaleFromMiddle
    
End If

Next ws

MsgBox "Process done!"

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
What are you basing that on?
Hi RoryA!

I think I got.

This line..

VBA Code:
Set sl = pres.Slides.Add(3, ppLayoutBlank)

Since it starts to paste the data at the 3rd slide, the first sheet will be pasted to slide 3, then the 2nd sheet will be pasted again on slide 3.
The first sheet that was copied first will be now on slide 4 that's why I thought it starts at the end of the sheet. My bad.

But how can I alter that line though? so that it would paste the excel data respectively starting slide 3?
Currently, I have 14 sheets.

Thank RoryA!
 
Upvote 0
You can use a variable and increment it each time, or you could simply add it to the end each time using pres.slides.count + 1 instead of 3?
 
Upvote 0
You can use a variable and increment it each time, or you could simply add it to the end each time using pres.slides.count + 1 instead of 3?
Hi RoryA!

Can you run me thru the usage of a variable? If you don't mind.
In the PPT presentation that I have, the first 2 slide was like the title/intro thingy.
That's why the pasting of excel data starts at slide 3.

Tried this
pres.slides.count + 1
but all of the data that was pasted was at the first slide disregarding the first 2 slide (which is my title/intro).
If you know what I mean.
 
Upvote 0
Hi Skyybot!!

I misunderstood RoryA's reply. (I'm sorry RoryA) I added this line and it WORKS however I forgot to mention that there's a 3 slides at the very end of my PPT. (yikes)
So it's like (1st & 2nd slide - for intro/title, 3rd-5th slide was another title/intro for different topic)
VBA Code:
Set sl = pres.Slides.Add(pres.Slides.Count + 1, ppLayoutBlank)
 
Upvote 0
So, am I to understand that you want to add a slide in between existing slides?
 
Upvote 0
Try
VBA Code:
Set sl = pres.Slides.Add(pres.Slides.Count - 3, ppLayoutBlank)
 
Upvote 1
Solution

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top