Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Sub averageScoreRelay()
  2.     ' 1. Run from PPT and open an Excel file
  3.    ' 2. Start at slide 1 and find a box that contains the words "iq_", if it has those words then it will have numbers after it like so "iq_43" or "iq_43, iq_56,iq_72".
  4.    ' 3. find those words and numbers in the opened Excel file after splitting and re-formating string.
  5.    ' 3. Copy column into a new sheets and repeat for all "iq_'s" until sheets 2 has a table.
  6.    ' 4. Copy table from xl Paste Table into ppt
  7.    ' 5. Do this for every slide
  8.  
  9.  
  10.  
  11. 'Timer start
  12.    Dim StartTime As Double
  13.     Dim SecondsElapsed As Double
  14.         StartTime = Timer
  15.        
  16.    
  17. 'Create variables
  18.    Dim xlApp As Excel.Application
  19.     Dim xlWB As Excel.Workbook
  20.     Dim ShRef As Excel.Worksheet
  21.     Dim ShWork As Excel.Worksheet
  22.     Dim pptPres As Object
  23.     Dim colNumb As Long
  24.  
  25.     ' Create new excel instance and open relevant workbook
  26.    Set xlApp = New Excel.Application
  27.     'xlApp.Visible = True 'Make Excel visible
  28.    Set xlWB = xlApp.Workbooks.Open("C:\Users\Andre Kunz\Desktop\Gate\Macros\averageScores\pptxlpratice\dummy2.xlsx", True, False, , , , True, Notify:=False) 'Open relevant workbook
  29.    If xlWB Is Nothing Then ' may not need this if statement. check later.
  30.        MsgBox ("Error retrieving Average Score Report, Check file path")
  31.         Exit Sub
  32.     End If
  33.     xlApp.DisplayAlerts = False
  34.      
  35.     'Find # of iq's in workbook
  36.    Set ShRef = xlWB.Worksheets("Sheet1")
  37.         colNumb = ShRef.Cells(1, ShRef.Columns.Count).End(xlToLeft).Column
  38.        
  39.     Dim IQRef() As String
  40.     Dim iCol As Long
  41.        
  42.     ReDim IQRef(colNumb)
  43.     ' capture IQ refs locally
  44.        For iCol = 2 To colNumb
  45.             IQRef(iCol) = ShRef.Cells(1, iCol).Value
  46.         Next iCol
  47.  
  48.     'Create a new blank Sheet in excel, should be "Sheet2"
  49.    xlWB.Worksheets.Add After:=xlWB.ActiveSheet
  50.     Set ShWork = xlWB.Worksheets("Sheet2")
  51.  
  52.     'Make pptPres the ppt active
  53.    Set pptPres = PowerPoint.ActivePresentation
  54.    
  55.     'Create variables for the slide loop
  56.    Dim pptSlide As Slide
  57.     Dim Shpe As Shape
  58.     Dim pptText As String
  59.     Dim iq_Array As Variant
  60.     Dim arrayLoop As Long
  61.     Dim myShape As Object
  62.     Dim k As Long
  63.     Dim i As Long
  64.     Dim lRows As Long
  65.     Dim lCols As Long
  66.    
  67.     'Loop through each pptSlide and check for IQ text box, grab avgScore values and create pptTable
  68.    For Each pptSlide In pptPres.Slides
  69.    
  70.         i = 0
  71.         pptSlide.Select
  72.  
  73.         'searches through shapes in the slide
  74.        For Each Shpe In pptSlide.Shapes
  75.        
  76.             If Not Shpe.HasTextFrame Then GoTo nextShpe 'boom, one less nested If statement
  77.            If Not Shpe.TextFrame.HasText Then GoTo nextShpe ' boom, another nested If statement bites the dust
  78.  
  79.             k = 1
  80.            
  81.                     'Set pptText as the Text in the box, then make it lowercase and trim Spaces and Enters
  82.                    pptText = Shpe.TextFrame.TextRange
  83.                     pptText = LCase(Replace(pptText, " ", vbNullString))
  84.                     pptText = Replace(Replace(Replace(pptText, vbCrLf, vbNullString), vbCr, vbNullString), vbLf, vbNullString)
  85.  
  86.  
  87.                     'Identify if within text there is "iq_"
  88.                    If InStr(1, pptText, "iq_") <= 0 Then GoTo nextShpe
  89.  
  90.                     'set iq_Array as an array of the split iq's
  91.                    iq_Array = Split(pptText, ",")
  92.  
  93.                     Dim hasIQs As Boolean
  94.                     Dim checkStr As String
  95.                     Dim pCol As Long
  96.                    
  97.                     hasIQs = iq_Array(0) Like "iq_#" Or iq_Array(0) Like "iq_##" Or iq_Array(0) Like "iq_###"
  98.                    
  99.                     If hasIQs Then
  100.                         ' paste inital column into temporary worksheet
  101.                        ShRef.Columns(1).Copy Destination:=ShWork.Columns(1)
  102.                     End If
  103.  
  104.                     ' loop for each iq_ in the array
  105.                    For arrayLoop = LBound(iq_Array) To UBound(iq_Array)
  106.                         ' Take copy of potential ref and adjust to standard if required
  107.                        checkStr = iq_Array(arrayLoop)
  108.                         If hasIQs And Left(checkStr, 3) <> "iq_" Then checkStr = "iq_" & checkStr
  109.  
  110.                         ' Look for existence of corresponding column in local copy array
  111.                        pCol = 0
  112.                         For iCol = 2 To colNumb
  113.                             If checkStr = IQRef(iCol) Then
  114.                                 pCol = iCol
  115.                                 Exit For
  116.                             End If
  117.                         Next iCol
  118.  
  119.                         If pCol > 0 Then
  120.                             ' Paste the corresponding column into the forming table
  121.                            k = k + 1
  122.                             ShRef.Columns(pCol).Copy Destination:=ShWork.Columns(k)
  123.                         End If
  124.  
  125.                     Next arrayLoop
  126.  
  127.     'calculate last row and last column on sheet2. aka. find Table size
  128.    With ShWork
  129.         lRows = .Cells(.Rows.Count, 1).End(xlUp).Row
  130.         lCols = .Cells(1, .Columns.Count).End(xlToLeft).Column
  131.  
  132.         'If only one column then go to next slide
  133.        If lRows = .Cells(1, 1).End(xlUp).Row And lCols = .Cells(1, 1).End(xlToLeft).Column Then
  134.             GoTo nextSlide
  135.         End If
  136.  
  137.             'Copy table
  138.            .Range(.Cells(1, 1), .Cells(lRows, lCols)).Copy
  139.     End With
  140.    
  141. tryAgain:
  142.  
  143.             ActiveWindow.ViewType = ppViewNormal
  144.             ActiveWindow.Panes(2).Activate
  145.             'Paste Table into ppt
  146.            pptSlide.Shapes.PasteSpecial DataType:=ppPasteHTML, Link:=msoFalse
  147.            
  148.             On Error GoTo tryAgain
  149.             On Error GoTo clrSht
  150.  
  151.             'Recently pasted shape is the last shape on slide, so it will be the same as count of shapes on slide
  152.            Set myShape = pptSlide.Shapes(pptSlide.Shapes.Count)
  153.                    
  154.             'Set position:
  155.            myShape.Left = -200
  156.             myShape.Top = 150 + i
  157.            
  158.             i = i + 150
  159.            
  160. clrSht:
  161.  
  162.             'Clear Sheet2 for next slide
  163.            ShWork.Range("A1:P10").Clear
  164.            
  165. nextShpe:
  166.                    
  167.         Next Shpe
  168.  
  169. nextSlide:
  170.  
  171.     Next pptSlide
  172.  
  173.     ShWork.Delete
  174.     xlWB.Close
  175.     xlApp.Quit
  176.  
  177.     xlApp.DisplayAlerts = True
  178.  
  179.     'End Timer
  180.    SecondsElapsed = Round(Timer - StartTime, 2)
  181.         MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
  182.  
  183. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement