Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     Option Explicit
  2.    
  3.     Private Sub averageScoreRelay()
  4.         ' 1. Run from PPT and open an Excel file
  5.        ' 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".
  6.        ' 3. find those words and numbers in the opened Excel file after splitting and re-formating string.
  7.        ' 3. Copy column into a new sheets and repeat for all "iq_'s" until sheets 2 has a table.
  8.        ' 4. Copy table from xl Paste Table into ppt
  9.        ' 5. Do this for every slide
  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 pptPres As Object
  22.         Dim colNumb As Long
  23.         Dim rowNumb 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\pinlop\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.         rowNumb = ShRef.Cells(ShRef.Rows.Count, 1).End(xlUp).Row
  39.            
  40.         Dim IQRef() As String
  41.         Dim iCol As Long
  42.         Dim IQRngRef() As Range
  43.            
  44.         ReDim IQRef(colNumb)
  45.         ReDim IQRngRef(colNumb)
  46.    
  47.         ' capture IQ refs locally
  48.        For iCol = 2 To colNumb
  49.             Set IQRngRef(iCol) = ShRef.Range(ShRef.Cells(1, iCol), ShRef.Cells(rowNumb, iCol))
  50.             IQRef(iCol) = ShRef.Cells(1, iCol).Value
  51.         Next iCol
  52.    
  53.         'Make pptPres the ppt active
  54.        Set pptPres = PowerPoint.ActivePresentation
  55.        
  56.         'Create variables for the slide loop
  57.        Dim pptSlide As Slide
  58.         Dim Shpe As Shape
  59.         Dim pptText As String
  60.         Dim iq_Array As Variant
  61.         Dim arrayLoop As Long
  62.         Dim myShape As Object
  63.         Dim i As Long
  64.         Dim lRows As Long
  65.         Dim lCols As Long
  66.         Dim k As Long
  67.        
  68.         'Loop through each pptSlide and check for IQ text box, grab avgScore values and create pptTable
  69.        For Each pptSlide In pptPres.Slides
  70.        
  71.             i = 0
  72.             pptSlide.Select
  73.    
  74.             'searches through shapes in the slide
  75.            For Each Shpe In pptSlide.Shapes
  76.            
  77.                 If Not Shpe.HasTextFrame Then GoTo nextShpe 'boom, one less nested If statement
  78.                If Not Shpe.TextFrame.HasText Then GoTo nextShpe ' boom, another nested If statement bites the dust
  79.                
  80.                 'Set pptText as the Text in the box, then make it lowercase and trim Spaces and Enters
  81.                pptText = Shpe.TextFrame.TextRange
  82.                 pptText = LCase(Replace(pptText, " ", vbNullString))
  83.                 pptText = Replace(Replace(Replace(pptText, vbCrLf, vbNullString), vbCr, vbNullString), vbLf, vbNullString)
  84.    
  85.    
  86.                 'Identify if within text there is "iq_"
  87.                If InStr(1, pptText, "iq_") <= 0 Then GoTo nextShpe
  88.    
  89.                 'set iq_Array as an array of the split iq's
  90.                iq_Array = Split(pptText, ",")
  91.    
  92.                 Dim hasIQs As Boolean
  93.                 Dim checkStr As String
  94.                 Dim pCol As Long
  95.                 Dim checkOne
  96.                        
  97.                 checkOne = iq_Array(0)
  98.                        
  99.                 hasIQs = Left(checkOne, 3) = "iq_"
  100.                
  101.                 Dim columnsToCopy As Collection
  102.                 Set columnsToCopy = New Collection
  103.                        
  104.                 If hasIQs Then
  105.                     ' paste inital column into temporary worksheet
  106.                    columnsToCopy.Add ShRef.Columns(1)
  107.                 End If
  108.    
  109.                 ' loop for each iq_ in the array
  110.                For arrayLoop = LBound(iq_Array) To UBound(iq_Array)
  111.                     ' Take copy of potential ref and adjust to standard if required
  112.                    checkStr = iq_Array(arrayLoop)
  113.                     If hasIQs And Left(checkStr, 3) <> "iq_" Then checkStr = "iq_" & checkStr
  114.    
  115.                     ' Look for existence of corresponding column in local copy array
  116.                    pCol = 0
  117.                     For iCol = 2 To colNumb
  118.                         If checkStr = IQRef(iCol) Then
  119.                             pCol = iCol
  120.                             Exit For
  121.                         End If
  122.                     Next iCol
  123.    
  124.                     If pCol > 0 Then
  125.                         ' Paste the corresponding column into the forming table
  126.                        columnsToCopy.Add ShRef.Columns(pCol).EntireColumn
  127.                     End If
  128.    
  129.                 Next arrayLoop
  130.    
  131.                 If columnsToCopy.Count > 1 Then                   'data was added
  132.                    ' Copy table
  133.                    
  134.     Dim unionVariable As Range
  135.  
  136.     Set unionVariable = columnsToCopy(1)
  137.  
  138.  
  139.     For k = 1 To columnsToCopy.Count
  140.         Set unionVariable = xlApp.Union(unionVariable, columnsToCopy(k))
  141.     Next k
  142.                    
  143.     unionVariable.Copy               ' all the data added to ShWork
  144.  
  145.                        
  146. tryAgain:
  147.                        
  148.                     ActiveWindow.ViewType = ppViewNormal
  149.                     ActiveWindow.Panes(2).Activate
  150.                        
  151.                     Set myShape = pptSlide.Shapes.PasteSpecial(DataType:=ppPasteHTML, Link:=msoFalse)
  152.                        
  153.                     On Error GoTo tryAgain
  154.                     On Error GoTo clrSht
  155.                        
  156.                     'Set position:
  157.                    myShape.Left = -200
  158.                     myShape.Top = 150 + i
  159.                     i = i + 150
  160.                    
  161.                 End If
  162.                
  163. clrSht:
  164.    
  165.                
  166.                
  167. nextShpe:
  168.                        
  169.             Next Shpe
  170.    
  171. nextSlide:
  172.    
  173.         Next pptSlide
  174.    
  175.         xlWB.Close
  176.         xlApp.Quit
  177.    
  178.         xlApp.DisplayAlerts = True
  179.    
  180.         'End Timer
  181.        SecondsElapsed = Round(Timer - StartTime, 2)
  182.         MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
  183.    
  184.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement