I have an open Excel file as well as an open PowerPoint file. I want to select and copy two different ranges in Excel (range1: B4:D9, range 2: F4:H10") into two shapes (tables) in my PowerPoint (range1 into Shape 16, range2 into Shape 20).
When I run the subs for range1 and range2 in debug mode after each other the data is pasted correctly in PowerPoint.
When I create one button for each range and use the button to execute the code it also works.
I would like to use the following code to call both codes for range1 and range2 to create only one button for this.
When I run the sub below (UpdateSlide) it pastes range F4:H10 into both Shapes 16 & 20.
Sub UpdateSlide()
Call range1
Call range2
End Sub
Code for range1 as well as range2:
Sub range1()
'Range1 in Excel
Dim range1 As Excel.range
Dim sheet As Excel.Worksheet
Dim excelApp As Excel.Application
Set excelApp = GetObject(, "Excel.Application")
Set sheet = excelApp.ActiveWorkbook.Sheets("Sheet1")
sheet.Activate
Set range1 = sheet.range("B4:D9")
range1.Select
range1.Copy
'Tabelle in Powerpoint auswählen
Dim table1 As Powerpoint.Shape
Dim pptApp As Powerpoint.Application
Set pptApp = GetObject(, "Powerpoint.Application")
pptApp.Activate
Dim slide As Powerpoint.slide
Set slide = pptApp.ActiveWindow.View.slide
Set table1 = slide.Shapes(16)
table1.table.Cell(1, 1).Select
'Daten einkopieren - ohne das Format verändert wird
pptApp.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
sheet.Activate
Set range1 = Nothing
Set sheet = Nothing
Set excelApp = Nothing
Set table1 = Nothing
Set pptApp = Nothing
Set slide = Nothing
End Sub
Sub range2()
'Range2 in Excel
Dim range2 As Excel.range
Dim sheet As Excel.Worksheet
Dim excelApp As Excel.Application
Set excelApp = GetObject(, "Excel.Application")
Set sheet = excelApp.ActiveWorkbook.Sheets("Sheet1")
sheet.Activate
Set range2 = sheet.range("F4:H10")
range2.Select
range2.Copy
'Tabelle in Powerpoint auswählen
Dim table2 As Powerpoint.Shape
Dim pptApp As Powerpoint.Application
Set pptApp = GetObject(, "Powerpoint.Application")
pptApp.Activate
Dim slide As Powerpoint.slide
Set slide = pptApp.ActiveWindow.View.slide
Set table2 = slide.Shapes(20)
table2.table.Cell(1, 1).Select
'Daten einkopieren - ohne das Format verändert wird
pptApp.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
Set range2 = Nothing
Set sheet = Nothing
Set excelApp = Nothing
Set table2 = Nothing
Set pptApp = Nothing
Set slide = Nothing
End Sub