Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
256
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\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.        
  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 sld As Slide
  57.     Dim shpe As Shape
  58.     Dim cTitle 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 iOffset As Long
  64.     Dim lRows As Long
  65.     Dim lCols As Long
  66.    
  67.     'Loop through each sld and check for chart title, grab avgScore values and create pptTable to paste into ppt chart
  68.    For Each sld In pptPres.Slides
  69.    
  70.         iOffset = 0
  71.         sld.Select
  72.  
  73.         'searches through shapes in the slide
  74.        For Each shpe In sld.Shapes
  75.        
  76.             If Not shpe.HasChart Then GoTo nxtShpe
  77.             If Not shpe.Chart.HasTitle Then GoTo nxtShpe
  78.            
  79.             Set c = shpe.Chart
  80.            
  81.             If c.ChartType = xlPie Then GoTo nxtShpe
  82.  
  83.             outCol = 1
  84.            
  85.                     'Set cTitle as the Text in the title, then make it lowercase and trim Spaces and Enters
  86.                    cTitle = c.ChartTitle.Text
  87.                     cTitle = LCase(Replace(cTitle, " ", vbNullString))
  88.                     cTitle = Replace(Replace(Replace(cTitle, vbCrLf, vbNullString), vbCr, vbNullString), vbLf, vbNullString)
  89.  
  90.  
  91.                     'Identify if within text there is "iq_"
  92.                    If InStr(1, cTitle, "iq_") <= 0 Then GoTo nextShpe
  93.  
  94.                     'set iq_Array as an array of the split iq's
  95.                    iq_Array = Split(cTitle, ",")
  96.  
  97.                     Dim hasIQs As Boolean
  98.                     Dim checkStr As String
  99.                     Dim pCol As Long
  100.                     Dim checkOne
  101.                    
  102.                     checkOne = iq_Array(0)
  103.                    
  104.                     hasIQs = Left(checkOne,3) = "iq_"
  105.                    
  106.                     If hasIQs Then
  107.                         ' paste inital column into temporary worksheet
  108.                        ShRef.Columns(1).Copy Destination:=ShWork.Columns(1)
  109.                     End If
  110.  
  111.                     ' loop for each iq_ in the array
  112.                    For arrayLoop = LBound(iq_Array) To UBound(iq_Array)
  113.                         ' Take copy of potential ref and adjust to standard if required
  114.                        checkStr = iq_Array(arrayLoop)
  115.                         If hasIQs And Left(checkStr, 3) <> "iq_" Then checkStr = "iq_" & checkStr
  116.  
  117.                         ' Look for existence of corresponding column in local copy array
  118.                        pCol = 0
  119.                         For iCol = 2 To colNumb
  120.                             If checkStr = IQRef(iCol) Then
  121.                                 pCol = iCol
  122.                                 Exit For
  123.                             End If
  124.                         Next iCol
  125.  
  126.                         If pCol > 0 Then
  127.                             ' Paste the corresponding column into the forming table
  128.                            outCol = outCol + 1
  129.                             ShRef.Columns(pCol).Copy Destination:=ShWork.Columns(outCol)
  130.                         End If
  131.  
  132.                     Next arrayLoop
  133.  
  134.                 If outCol > 1 Then 'data was added
  135.                     ' Copy table
  136.                     ShWork.UsedRange.Copy ' all the data added to ShWork
  137.            
  138.     With c.ChartData
  139.         .Activate
  140.         .Workbooks.Sheets(1).UsedRange.Clear
  141.         .Workbooks.Sheets(1).Range(A2).Paste
  142.         .Workbooks.Close
  143.     End With                   
  144.                    
  145.                     ' Clear data from temporary sheet
  146.                     ShWork.UsedRange.Clear
  147.                 End If  
  148.            
  149. clrSht:
  150.  
  151.             'Clear Sheet2 for next slide
  152.            ShWork.Range("A1:P10").Clear
  153.            
  154. nextShpe:
  155.                    
  156.         Next shpe
  157.  
  158. nextSlide:
  159.  
  160.     Next sld
  161.  
  162.     ShWork.Delete
  163.     xlWB.Close
  164.     xlApp.Quit
  165.  
  166.     xlApp.DisplayAlerts = True
  167.  
  168.     'End Timer
  169.    SecondsElapsed = Round(Timer - StartTime, 2)
  170.         MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
  171.  
  172. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement