简体   繁体   English

VBA循环遍历表

[英]VBA Loop to iterate over a table

I am trying to automate a very manual and annoying process. 我正在尝试自动化一个非常手动和烦人的过程。

The situation:I have a table in an excel worksheet, with source location for chart objects within the workbook, their target formatting specs, and their target location on a given powerpoint. 情况:我在excel工作表中有一个表,其中包含工作簿中图表对象的源位置,它们的目标格式规范以及它们在给定Powerpoint上的目标位置。

so far ive set up the code as follows. 到目前为止,我将代码设置如下。 Ultimately i would like a loop to iterate over the entire cells of the table and pick up the necessary details to fulfill the below code. 最终,我希望循环遍历表的整个单元格,并选择必要的细节来实现以下代码。 Ideally, replacing all italics with variables extracted from the table. 理想情况下,将所有斜体替换为从表中提取的变量。

Please help!! 请帮忙!!

Dim shP As Object
Dim myShape As Object
Dim mySlide As Object
Dim tempSize As Integer, tempFont As String

Worksheets("Sheet 1").Activate

'select the name of report
Set shP = Range(V.Title)

'select the ppt sheet you wish to copy the object to
Set mySlide = PPT.ActivePresentation.slides(1)

'count the number of shapes currently on the PPT
shapeCount = mySlide.Shapes.Count
'copy the previously selected shape
shP.Copy
'paste it on the PPT
mySlide.Shapes.Paste

'wait until the count of shapes on the PPT increases, which signals that the past operation is finished.
Do '<~~ wait completion of paste operation
    DoEvents
Loop Until mySlide.Shapes.Count > shapeCount

'adjust formatting of the newly copied shape: position on the sheet, font & size
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)

    myShape.Left = 254.016
    myShape.Top = 42.8085
    myShape.Width = 286.0515
    myShape.Height = 46.7775
    myShape.TextEffect.FontSize = 15
    myShape.TextEffect.FontName = "Century Schoolbook"

Make a procedure out of it using your desired variables as parameters: 使用所需的变量作为参数来制作一个过程:

Sub MyProcedure(RangeTitle As String, SlideNumber As Long, SetLeft As Double, SetTop As Double, SetWidth As Double, SetHeight As Double)
    Dim shP As Object
    Dim myShape As Object
    Dim mySlide As Object
    Dim tempSize As Integer, tempFont As String

    Dim ws As Worksheet
    Set ws = Worksheets("Sheet 1")

    'select the name of report
    Set shP = ws.Range(RangeTitle)

    'select the ppt sheet you wish to copy the object to
    Set mySlide = PPT.ActivePresentation.slides(SlideNumber)

    'count the number of shapes currently on the PPT
    shapeCount = mySlide.Shapes.Count
    'copy the previously selected shape
    shP.Copy
    'paste it on the PPT
    mySlide.Shapes.Paste

    'wait until the count of shapes on the PPT increases, which signals that the past operation is finished.
    Do '<~~ wait completion of paste operation
        DoEvents
    Loop Until mySlide.Shapes.Count > shapeCount

    'adjust formatting of the newly copied shape: position on the sheet, font & size
    Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
    With myShape
        .Left = SetLeft
        .Top = SetTop
        .Width = SetWidth
        .height = SetHeight
        .TextEffect.FontSize = 15
        .TextEffect.FontName = "Century Schoolbook"
    End With
End Sub

Then you can use that procedure easily in a loop: 然后,您可以轻松地在循环中使用该过程:

Sub LoopThrougMyData()

    Dim iRow As Long
    For iRow = FirstRow To LastRow 'loop through your table here

        With Worksheets("YourDataTable")
            MyProcedure RangeTitle:=.Cells(iRow, "A"), SlideNumber:=.Cells(iRow, "B"), SetLeft:=.Cells(iRow, "C"), SetTop:=.Cells(iRow, "D"), SetWidth:=.Cells(iRow, "E"), SetHeight:=.Cells(iRow, "F")
            'call the procedure with the data from your table
        End With

    Next iRow

End Sub

Edit (see comment) 编辑(请参阅评论)

I would add another parameter for the PPT file: 我将为PPT文件添加另一个参数:

Sub MyProcedure(PPT As Object, RangeTitle As String, SlideNumber As Long, …

So you can open the PowerPoint in LoopThrougMyData and provide it as parameter to your procedure MyProcedure 因此,您可以在LoopThrougMyData打开PowerPoint并将其作为参数提供给过程MyProcedure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM