Search code examples
excelvbapowerpoint

How to access data from Excel cells in PowerPoint (using VB 6.3)


I would like to access data from Excel to be used in powerpoint. I have found this tutorial https://www.datanumen.com/blogs/2-effective-methods-to-extract-numbers-from-your-excel-cells/, but it describes how to use it in the Excel alone:

Sub ExtractNumbersFromCells()
  Dim objRange As Range, nCellLength As Integer, nNumberPosition As Integer, strTargetNumber As String

  '  Initialization
  strTargetNumber = ""

  '  Go through all the cells in the target range
  For Each objRange In Range("B4:B14")
    nCellLength = Len(objRange)

    '  Extract numbers from cells
    For nNumberPosition = 1 To nCellLength
      If IsNumeric(Mid(objRange, nNumberPosition, 1)) Then
        strTargetNumber = strTargetNumber & Mid(objRange, nNumberPosition, 1)
      End If
    Next nNumberPosition

    objRange.Offset(0, 1) = strTargetNumber
    strTargetNumber = ""
  Next objRange
End Sub

So for example I have my values on rows: B4:B14 ... and I want to process them in PowerPoint. How to do that?


Solution

  • Here's a very basic example of automating Excel from PPT:

    'Requires VBA Project reference to "Microsoft Excel xx.0 Object Library"
    Sub Tester()
    
        Dim xlApp As Excel.Application
        Dim wb As Excel.Workbook
        Dim ws As Excel.Worksheet
        Dim x As Long
        
        Set xlApp = New Excel.Application    'open Excel
        xlApp.Visible = True                 'make visible
        
        Set wb = xlApp.Workbooks.Open("C:\Temp\Test.xlsx") 'open a workbook
        Set ws = wb.Worksheets("Sheet1")                   'get a worksheet reference
        
        For x = 1 To 5                       'loop over a range
            Debug.Print ws.Cells(x, 1).Value 'read cell value
        Next x
        
        wb.Close False                       'don't save changes
        xlApp.Quit                           'close excel
        
    End Sub
    

    Most of the VBA examples you'll find online cover automating PPT from Excel (since automating the creation of presentations using Excel data is a common use case).