Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
133
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 outCol 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.             outCol = 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.                     Dim checkOne
  97.                    
  98.                     checkOne = iq_Array(0)
  99.                    
  100.                     hasIQs = Left(checkOne,3) = "iq_"
  101.                    
  102.                     If hasIQs Then
  103.                         ' paste inital column into temporary worksheet
  104.                        ShRef.Columns(1).Copy Destination:=ShWork.Columns(1)
  105.                     End If
  106.  
  107.                     ' loop for each iq_ in the array
  108.                    For arrayLoop = LBound(iq_Array) To UBound(iq_Array)
  109.                         ' Take copy of potential ref and adjust to standard if required
  110.                        checkStr = iq_Array(arrayLoop)
  111.                         If hasIQs And Left(checkStr, 3) <> "iq_" Then checkStr = "iq_" & checkStr
  112.  
  113.                         ' Look for existence of corresponding column in local copy array
  114.                        pCol = 0
  115.                         For iCol = 2 To colNumb
  116.                             If checkStr = IQRef(iCol) Then
  117.                                 pCol = iCol
  118.                                 Exit For
  119.                             End If
  120.                         Next iCol
  121.  
  122.                         If pCol > 0 Then
  123.                             ' Paste the corresponding column into the forming table
  124.                            outCol = outCol + 1
  125.                             ShRef.Columns(pCol).Copy Destination:=ShWork.Columns(outCol)
  126.                         End If
  127.  
  128.                     Next arrayLoop
  129.  
  130.                 If outCol > 1 Then 'data was added
  131.                     ' Copy table
  132.                     ShWork.UsedRange.Copy ' all the data added to ShWork
  133.                    
  134. tryAgain:
  135.                    
  136.                     ActiveWindow.ViewType = ppViewNormal
  137.                     ActiveWindow.Panes(2).Activate
  138.                    
  139.                     Set myShape = pptSlide.Shapes.PasteSpecial(DataType:=ppPasteHTML, Link:=msoFalse)
  140.                    
  141.                     On Error GoTo tryAgain
  142.                     On Error GoTo clrSht
  143.                    
  144.                     'Set position:
  145.                     myShape.Left = -200
  146.                     myShape.Top = 150 + i
  147.                     i = i + 150
  148.  
  149.                     ' Clear data from temporary sheet
  150.                     ShWork.UsedRange.Clear
  151.                 End If  
  152.            
  153. clrSht:
  154.  
  155.             'Clear Sheet2 for next slide
  156.            ShWork.Range("A1:P10").Clear
  157.            
  158. nextShpe:
  159.                    
  160.         Next Shpe
  161.  
  162. nextSlide:
  163.  
  164.     Next pptSlide
  165.  
  166.     ShWork.Delete
  167.     xlWB.Close
  168.     xlApp.Quit
  169.  
  170.     xlApp.DisplayAlerts = True
  171.  
  172.     'End Timer
  173.    SecondsElapsed = Round(Timer - StartTime, 2)
  174.         MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
  175.  
  176. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement