2

I want to create a Chart using Excel data.

How can I do that?

4
  • Not sure, but .XValues = xlBook.Sheets(1).Range("A2:A9") i think, looking at learn.microsoft.com/en-us/office/vba/api/excel.series.xvalues Commented Sep 16 at 10:49
  • What is the error? Commented Sep 16 at 11:27
  • Is the first sheet in the workbook a worksheet or a chart sheet? You may use .Worksheets instead of .Sheets. Commented Sep 16 at 13:34
  • Have you tried creating the chart in Excel and bringing it in Powerpoint after? Commented Sep 16 at 14:27

1 Answer 1

1

Try

Option Explicit

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
'Public xlApp As Excel.Application
'Public xlBook As Excel.Workbook

Public Sub Macro1()
    
    Dim xlApp, xlBook, i, arData, ws
    
    ' get Excel data
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlBook = xlApp.Workbooks.Open(FileName:="Data.xlsx", ReadOnly:=True)
    arData = xlBook.sheets(1).Range("A1:B6")
    xlBook.Close False
    xlApp.Quit
    
    'Delete all Slides
    For i = ActivePresentation.Slides.Count To 1 Step -1
        ActivePresentation.Slides(i).Delete
    Next i
    
    'Add a blank slide
    Dim ppSlide As Slide, ppShape As Shape
    Set ppSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
    
    'Add a Map Chart
    Set ppShape = ppSlide.Shapes.AddChart2(Style:=140, Type:=xlRegionMap, _
           Left:=0, Top:=0, Width:=960, Height:=540, NewLayout:=False)
    ppShape.Chart.Name = "myChart"

    ' wait for chartdata sheet
    On Error Resume Next
    Do
        DoEvents
        Sleep 1000
        Set ws = ppShape.Chart.ChartData.Workbook.sheets(1)
    Loop While IsEmpty(ws)
    On Error GoTo 0
    
    ' copy data into PP
    With ws
       .UsedRange.Clear
       .Range("A1").Resize(UBound(arData), UBound(arData, 2)) = arData
       ws.Parent.Close
    End With
    
    MsgBox "Complete"
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.