Results 1 to 3 of 3

Run Time Error 91 'Oject variable or With block variable not set

Threaded View

  1. #1
    Registered User
    Join Date
    08-09-2012
    Location
    INDIA
    MS-Off Ver
    Excel 2007
    Posts
    2

    Run Time Error 91 'Oject variable or With block variable not set

    I am trying to create a pie chart using VBA. I wrote the below mentioned code but it gives me error 91 while executing. Can anyone help me solve this issue

    Code:

    Private Sub CommandButton1_Click()
    
    Set excel1 = CreateObject("Excel.Application")
    Set worbook = excel1.Workbooks
    excel1.Workbooks.Add
    
    excel1.Visible = True
    Set ows = excel1.ActiveSheet
    
    'enter the data in newly created excel
    ows.Cells(2, 3).Value = "a"
    ows.Cells(2, 4).Value = 23
    ows.Cells(3, 3).Value = "b"
    ows.Cells(3, 4).Value = 34
    ows.Cells(4, 3).Value = "c"
    ows.Cells(4, 4).Value = 32
    ows.Cells(5, 3).Value = "d"
    ows.Cells(5, 4).Value = 30
    
    'Add a pie chart to the excel
    
    ows.Shapes.AddChart(XlChartType.xlPie, 400, 200, 250, 150).Select
    Set ocht = ActiveChart
    Set oRng = ows.Range("c2", "d5")
    ocht.SetSourceData Source:=oRng, PlotBy:=xlColumns  ' ERROR is OBTAINED at this line
    ocht.HasLegend = False
    ocht.ChartTitle.Text = "Sample PieChart with Legend and Title"
    
    End Sub
    Last edited by Cutter; 08-09-2012 at 06:15 PM. Reason: Added code tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1