У меня есть данные Excel, содержащие диапазон от A40 до G1330, но фактический диапазон от A40 до G63, поскольку в оставшемся диапазоне есть формула, но ячейка пуста: «Я применил формулу ЕСЛИОШИБКА, сделав ячейку пустой»
также у меня есть существующий файл PowerPoint, и слайд номер 3 содержит таблицу с именем «Таблица 1».
я хочу заполнить таблицу, которая находится в файле PowerPoint, из диапазона Excel от D40 до E63.
Я попробовал следующий код
Dim Name_Attendees_lastVal As Range
Dim sht As Worksheet
Dim pptapp As PowerPoint.Application
Dim presentation As PowerPoint.presentation
Dim ppslide As PowerPoint.Slide
Dim slidetitle As String
Dim pptfile As String
Dim slideCtr As Integer
Set sht = Sheets("Reports")
Set Name_Attendees_lastVal = sht.Columns(5).Find("*", sht.Cells(1, 2), xlValues, xlPart, xlByColumns,xlPrevious)
sht.Range("D40", Name_Attendees_lastVal).Resize(, 2).Select
pptfile = "C:\Users\habinalshaikh\Desktop\Training\presentation maker\Course Report.pptx"
Set pptapp = CreateObject("PowerPoint.Application")
pptapp.Visible = True
pptapp.Presentations.Open (pptfile)
for i = 40 to Set Name_Attendees_lastVal = sht.Columns(5).Find("*", sht.Cells(1, 2), xlValues, xlPart, xlByColumns, xlPrevious).count ' this code is to count the number of rows, but it doesn't work
pptapp.ActivePresentation.Slides(3).Shapes("Table 1").TextFrame.TextRange.Characters.Text = ThisWorkbook.Sheets("Reports").Range("D"&i) ' this code is to fill the table which is in the PowerPoint file with the names in the excel sheet
next
for y = 40 to Set Name_Attendees_lastVal = sht.Columns(5).Find("*", sht.Cells(1, 2), xlValues, xlPart, xlByColumns, xlPrevious).count ' this code is to count the number of rows, but it doesn't work
pptapp.ActivePresentation.Slides(3).Shapes("Table 1").TextFrame.TextRange.Characters.Text = ThisWorkbook.Sheets("Reports").Range("E"&i)' this code is to fill the table which is in the PowerPoint file with the Attending status in the excel sheet
next
В таблице 1 есть 2 столбца. И я хочу вставить из D40, чтобы последняя строка содержала значение «не пусто» в таблице 1. То же самое с E40.
В коде ниже есть синтаксическая ошибка.
' this code is to count the number of rows, but it doesn't work
for i = 40 to Set Name_Attendees_lastVal = sht.Columns(5).Find("*", sht.Cells(1, 2), xlValues, xlPart, xlByColumns, xlPrevious).count
Set
нельзя использовать в предложении For
Find
возвращает либо Nothing
, либо объект Range
; если он возвращает объект Range
, свойство Row
используется для получения номера строки.After
) Find
должен находиться в пределах искомого диапазона (Columns(5)
); в противном случае произойдет ошибка выполнения 13.Документация Майкрософт:
Option Explicit
Sub Excel2PPTTable()
Dim Name_Attendees_lastVal As Range
Dim oSht As Worksheet
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.presentation
Dim pptSlide As PowerPoint.Slide
Dim pptTab As PowerPoint.Table
Dim pptfile As String
Dim iR As Long, i As Long
Const START_ROW = 2 ' change to 1 if there isn't a header row in Table1
Set oSht = Sheets("Reports")
pptfile = "C:\Users\habinalshaikh\Desktop\Training\presentation maker\Course Report.pptx"
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = True
Set pptPres = pptApp.Presentations.Open(pptfile)
Set pptTab = pptPres.Slides(3).Shapes("Table 1").Table
If pptTab Is Nothing Then
MsgBox "Can't find table on the third slide."
Else
With oSht.Columns(4)
Set Name_Attendees_lastVal = .Find("*", .Cells(1), xlValues, xlPart, xlByRows, xlPrevious)
End With
iR = START_ROW - 1
For i = 40 To Name_Attendees_lastVal.Row
iR = iR + 1
pptTab.Cell(iR, 1).Shape.TextFrame2.TextRange.Text = oSht.Range("D" & i) ' the names in the excel sheet
pptTab.Cell(iR, 2).Shape.TextFrame2.TextRange.Text = oSht.Range("E" & i) ' the Attending status in the excel sheet
Next
End If
' pptPres.Save ' save file
' pptPres.Close ' close file
' pptApp.Quit ' close ppt app.
End Sub
Сколько столбцов в
Table 1
? Хотите вставить содержимое ячеек D40 и E40 в одну строкуTable 1
?