Results 1 to 5 of 5

Thread: [RESOLVED] excel VBA multiple chart problem

  1. #1

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Resolved [RESOLVED] excel VBA multiple chart problem

    Currently I have a number of checkboxes, which the user clicks to graph certain data. Obviously in some instances, more than one checkbox is checked, so I would like to graph more than one graph, however with the following code I have, every graph after the first one (which works fine) fails with run-time error 1004 "Application - defined or Object- defined error". The line .setSourceData is highlighted with that error. Any help? Do I need to make a chartObjects?

    Thanks much.


    Code:
    Sub AddChartSheet(chartName As String, setRange As String, yTitle As String, graphStyle As Integer)
       Dim chtChart as Chart
       Set chtChart = Charts.Add
       With chtChart
          .Name = chartName
          If graphStyle = 2 Then
            .ChartType = xlColumnClustered
            
          ElseIf graphStyle = 1 Then
            .ChartType = xlLine
           
          End If
          .SetSourceData Source:=Sheets("SOILSYM_MONTH").Range(setRange), _
            PlotBy:=xlColumns
          'Link to the source data range.
          .HasTitle = True
          .ChartTitle.Text = chartName
          .Axes(xlCategory, xlPrimary).HasTitle = True
          .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Month"
          .Axes(xlValue, xlPrimary).HasTitle = True
          .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = yTitle
          .SeriesCollection(1).Delete
       End With
    End Sub

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: excel VBA multiple chart problem

    Hi

    I could be wrong but

    Whilst the code stops at ".SetSourceData " with this error it is probably NOT due to that. The other reason that I can think of is because of "chartName"

    It is possible that while creating a chart you are not naming the chart correctly so the first time you create a chart using a name say "xyz", you cannot use the same name again. If you do then you will get the error which you are getting...

    Try changing the name of all the charts that you are creating for example

    vb Code:
    1. If CheckBox1.Value = "aaa" Then 'just an example
    2.     .Name = "aaa"
    3. ElseIf CheckBox1.Value = "bbb" Then 'just an example
    4.     .Name = "bbb"
    5. End If

    I think this should solve the problem... but like I said, I could be wrong...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: excel VBA multiple chart problem

    Hrmm I don't think that is the problem because I pass "chartName" into the method and set .name = to that string, and I don't pass the same name twice into the method.

  4. #4

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: excel VBA multiple chart problem

    and also I think you get a slightly different error msg if you try to name a sheet the same name as another.

  5. #5

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: excel VBA multiple chart problem

    actually figured it out... had to do with not resetting my range variable. So on multiple graphs range kept getting appended new info, making it an illegal range.

    thanks for the help though

Posting Permissions

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



Click Here to Expand Forum to Full Width