VBA PowerPoint - Lag PowerPoint-presentasjon fra Excel ved hjelp av VBA

Innholdsfortegnelse:

Anonim

Excel VBA PowerPoint-opplæring

VBA er et kraftig verktøy som kan brukes innen ethvert område av Microsoft integrerte verktøy. I likhet med MS Excel har Powerpoint også en bestemmelse for å lage en makro og automatisere presentasjonen. Automatiseringen kan være av hvilken som helst form. Du har sett presentasjoner der lysbildene er så store og detaljerte, som noen ganger ender opp med å ha 50+ lysbilder bare for å dekke et treningsemne eller en rapport. Så i stedet for å legge til diagrammer i PowerPoint fra Excel, kan vi lage en kode som direkte vil kopiere diagrammer fra Excel og lime den inn i PowerPoint-lysbilde.

Hvordan lage en PowerPoint-presentasjon fra Excel VBA?

Nedenfor er eksemplet for å lage en powerpoint-presentasjon ved hjelp av VBA-kode i Excel:

Du kan laste ned denne VBA PowerPoint Excel-malen her - VBA PowerPoint Excel-mal

VBA PowerPoint-eksempel

La oss lage et diagram først i Excel. For det trenger vi data. Nedenfor har vi noen salgsdata for 8 selgere.

La oss nå lage et kolonnediagram eller diagram ved hjelp av dataene ovenfor. Vi ser nedenfor, vi har nå et kolonnediagram med tittelen solgt mengde og alle etiketter satt inn.

Nå er vår oppgave å få denne grafen i PowerPoint-lysbilde slik den vises her med tittelen på diagrammet som tittelen på PowerPoint-lysbilde. For dette må vi aktivere PowerPoint i VBA.

Følg trinnene nedenfor:

Trinn 1: Gå til VBA Tool- menyen som vist nedenfor, og velg Referanser … -alternativet som vist nedenfor.

Trinn 2: Når vi har gjort det, får vi et referanser VBA Project-vinduer. Velg MS PowerPoint 15.0 Object Library fra listen, som vist nedenfor. Dette vil aktivere alle kommandoene relatert til MS PowerPoint i VBA. Uten dette kan vi ikke kjøre VBA i PowerPoint. Merk av i boksen med nevnte bibliotek og klikk på Ok .

Trinn 3: Nå for å skrive koden for VBA PowerPoint, trenger vi en modul. For å få en ny modul, gå til Sett inn-menyen og velg et modulalternativ som vist nedenfor.

Trinn 4: Skriv underkategorien til VBA PowerPoint eller i et hvilket som helst annet navn etter behov, som vist nedenfor.

Kode:

 Sub VBA_Presentation () Slutt Sub 

Trinn 5: Nå for å lage en presentasjon ved hjelp av Chart in excel, trenger vi få variabler. La oss vurdere 5 variabler som:

  1. PA-søknad for PowerPoint-applikasjon.
  2. PPT for PowerPoint-presentasjon,
  3. PPTSlide for PowerPoint-lysbilde,
  4. PPTS-former for PowerPoints,
  5. PPTCharts for Excel Chart Object.

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject End Sub 

Trinn 6: BrukSet- kommandoen for å legge til en ny PowerPoint- applikasjon som vist nedenfor.

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject Set PAplication = New PowerPoint.Application End Sub 

Trinn 7: Gjør PowerPoint-applikasjonen synlig i en kodelinje og bruk msoCTrue til mystisk vurdering av feil måte. Og etter det vil den samme applikasjonen bli brukt som maksimert i PowerPoint for å få full oversikt.

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject Set PAplication = New PowerPoint.Application PAplication.Visible = msoCTrue PAplicationWindow ppWindowMaximized End Sub 

Trinn 8: Nå angir PPT som er presentasjonen vår å legge til i MS PowerPoint-applikasjonen,

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject Set PAplication = New PowerPoint.Application PAplication.Visible = msoCTrue PAplicationWindow ppWindowMaximized Set PPT = PAplication.Presentations.Add End Sub 

Trinn 9: Nå vil vi bruke en kombinasjon av For-Next og If-Else loop. Starter med For-Next Loop.

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject Set PAplication = New PowerPoint.Application PAplication.Visible = msoCTrue PAplicationWindow ppWindowMaximized Set PPT = PAplication.Presentations.Tillegg for neste slutt sub 

Trinn 10: I For loop of PPTCharts, første aktive diagrammer i Excel.

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject Set PAplication = New PowerPoint.Application PAplication.Visible = msoCTrue PAplicationWindow ppWindowMaximized Set PPT = PAplication.Presentations.Tillegg for hvert PPTCharts i ActiveSheet.ChartObjects Neste PPTCharts End Sub 

Trinn 11: Nedenfor er koden for å lime inn diagrammet fra Excel til PowerPoint-lysbilde. Bruk kode for å legge til et lysbilde i definert PAplikasjon og legge til + 1 lysbilde hver gang vi kjører koden.

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject Set PAplication = New PowerPoint.Application PAplication.Visible = msoCTrue PAplicationWindow ppWindowMaximized Sett PPT = PAplication.Presentations.Tillegg for hver PPTCharts i ActiveSheet.ChartObjects PAplication.ActivePresentation.Slides.Add PAplication.ActivePresentation.Slides.Count + 1, ppLayoutText Neste PPTCharts End Sub Sub 

Trinn 12: Nå på fortsettelsen til neste kodelinje, bruk kortsettet nedenfor for å angi et aktivt vindusvisning. Dette vil ta i bruk for å skyve etter at Chart blir limt inn i PowerPoint-presentasjonen.

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject Set PAplication = New PowerPoint.Application PAplication.Visible = msoCTrue PAplicationWindow ppWindowMaximized Set PPT = PAplication.Presentations.Legg til for hvert PPTCharts i ActiveSheet.ChartObjects PAplication.ActivePresentation.Slides.Add PAplication.ActivePresentation.Slides.Count + 1, ppLayoutText PAplication.ActiveWindow.View.PlateSplide = PAplication.ActivePresentation.Slides (PAplication.ActivePresentation.Slides.Count) Neste PPTCharts End Sub 

Trinn 13: Når du er ferdig, velger du PPTChart- variabelen som vi definerte tidligere. Etter det kopierer du det valgte aktive diagrammet til kartområdet der det vil bli plassert. Og bruk Lim inn spesialkommando for å lime inn diagrammet med bilder.

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject Set PAplication = New PowerPoint.Application PAplication.Visible = msoCTrue PAplicationWindow ppWindowMaximized Set PPT = PAplication.Presentations.Legg til for hvert PPTCharts i ActiveSheet.ChartObjects PAplication.ActivePresentation.Slides.Add PAplication.ActivePresentation.Slides.Count + 1, ppLayoutText PAplication.ActiveWindow.View.PlateSplide = PAplication.ActivePresentation.Slides (PAplication.ActivePresentation.Slides.Count) PPTCharts.Select ActiveChart.ChartArea.Copy PPTSlide.Shapes.PasteSpecial (DataType: = ppPasteMetafilePicture) .Velg neste PPTCharts End Sub Sub 

Trinn 14: Velg nå formen på diagrammet som er i første posisjon som tekstområde. Og ha den samme tittelen som er " solgt mengde " i PowerPoint-presentasjonen.

Kode:

 Sub VBA_Presentation () Dim PAplication As PowerPoint.Application Dim PPT As PowerPoint.Presentation Dim PPTSlide As PowerPoint.Slide Dim PPTShapes As PowerPoint.Shape Dim PPTCharts As Excel.ChartObject Set PAplication = New PowerPoint.Application PAplication.Visible = msoCTrue PAplicationWindow ppWindowMaximized Set PPT = PAplication.Presentations.Legg til for hvert PPTCharts i ActiveSheet.ChartObjects PAplication.ActivePresentation.Slides.Add PAplication.ActivePresentation.Slides.Count + 1, ppLayoutText PAplication.ActiveWindow.View.PlateSplide = PAplication.ActivePresentation.Slides (PAplication.ActivePresentation.Slides.Count) PPTCharts.Select ActiveChart.ChartArea.Copy PPTSlide.Shapes.PasteSpecial (DataType: = ppPasteMetafilePicture) .Velg PPTSlide.ShapexthFext.Range .Chart.ChartTitle.Text Neste PPTCharts End Sub 

Dette fullfører koden for VBA PowerPoint.

Trinn 15: Sett sammen koden trinnvis for å vite om noen kodelinje har en feil ved å trykke på funksjonstasten F8. Og kjør deretter koden ved å klikke på Play-knappen som er under menylinjen som vist nedenfor.

Vi vil få diagrammet lagt ut i PowerPoint-filen i det første lysbildet av det som vist nedenfor.

Siden koden er stor, så kan den komplette koden sees i tekstboksen nedenfor.

Fordeler med Excel VBA PowerPoint

  • Å bruke VBA i Powerpoint er enkelt å håndtere hvis en ppt-fil har så mange lysbilder med stort innhold.
  • VBA med Powerpoint gir et snev av automatisering selv med begrensede funksjoner tilgjengelig.

Ulemper med Excel VBA PowerPoint

  • Vi må velge Microsoft PowerPoint 15.0 Object Library fra Reference-alternativet i Tool-menyalternativet, som vi trenger i starten av eksempel-1, hver gang vi kjører koden for PowerPoint.

Ting å huske

  • Lagre filen i makroaktiver presentasjonsformat etter at du har skrevet koden. Dette vil hjelpe oss å unngå å miste koden og bruke den samme flere tiden i fremtiden.
  • Omkodingsfunksjonen vil ikke fungere her, da vi trenger å hoppe fra Excel til PowerPoint og endre grensesnittet mellom sidene.
  • Sett alltid inn koden før du kjører. Dette vil hjelpe deg med å finne feilen i koden. Dette er ganske nyttig når vi skriver store kodelinjer.
  • For å kjøre og implementere koden må vi åpne excelarket med Chart at vi vil lime den inn i PowerPoint-lysbilde.
  • Vi kan justere diagrammet i PowerPoint etter våre behov.

Anbefalte artikler

Dette er en guide til VBA PowerPoint. Her diskuterer vi hvordan du lager PowerPoint-presentasjon fra Excel ved hjelp av VBA-kode sammen med et praktisk eksempel og nedlastbar Excel-mal. Du kan også gå gjennom andre foreslåtte artikler -

  1. VBA Delete Column
  2. Statuslinje i Excel
  3. VBA Fjern duplikater
  4. Lag regneark i Excel