使用VBA将数据从Excel单元提取到PowerPoint Shape

时间:2019-02-08 17:04:12

标签: vba powerpoint

我有一本excel工作簿,其中第一栏为朱利安日,第二栏为正常高温,第三栏为正常低温。我需要在PowerPoint VBA中编写一个脚本,以使当前的儒略日与工作簿的第一列中的数字相同。然后,我需要它来提取当天的正常高点(第二列),并将该数字插入到特定PPT幻灯片中的命名形状中。这是我到目前为止的内容:

Function SectionIndexOf(sSectionName As String) As Long
'This Function makes sure you can declare the name of any Section Name
'in the Sub below.

    Dim x As Long
    With ActivePresentation.SectionProperties
        For x = 1 To .Count
            If .Name(x) = sSectionName Then
                SectionIndexOf = x
            End If
        Next
    End With
End Function

Sub Climo()

Headlines = SectionIndexOf("Headlines")

'Open the Excel Workbook.
Dim CLI As New Excel.Workbook
Set CLI = Excel.Application.Workbooks.Open("Z:\climo.xlsx")

'Grab the first Worksheet in the Workbook
Dim WS As Excel.Worksheet
Dim NormalHi As String
Dim NormalLo As String
Dim shp As Shape
Dim sld As Slide

Set WS = CLI.Worksheets(1)

Dim i As Long
'Loop through all rows in Column A (Julian Day)

For i = 1 To WS.Range("A372").End(xlUp).Row
    NormalHi = WS.Cells(i, 9).Value
    Debug.Print NormalHi 'This just returns new blank lines

    For Each sld In ActivePresentation.Slides
        For Each shp In sld.Shapes
            If sld.sectionIndex = Headlines Then
                With sld.Shapes("NormalHi")
                .TextFrame2.TextRange.Font.Name = Arial
                .TextFrame2.TextRange.Font.Size = 16
                .TextFrame.TextRange.Font.Color = vbRed
                .TextFrame2.TextRange.Text = NormalHi
                End With
            End If
        Next
    Next
Next

End Sub

在试图弄清楚如何将当前的儒略日与Excel工作簿中的正确行匹配之前,我只是试图从任何单元格中提取数据并将其绘制为形状。当我运行此命令并使用Debug.Print时,没有出现任何空白。不知道出什么问题了。谢谢!

1 个答案:

答案 0 :(得分:1)

您在问题中提到在第2列中找到了Normal High数据,但是当您获取该值时,您将引用第9列。

NormalHi = WS.Cells(i, 9).Value