+ Reply to Thread
Results 1 to 6 of 6

VBA Error when running macro

Hybrid View

  1. #1
    Registered User
    Join Date
    07-19-2012
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    VBA Error when running macro

    Greetings,

    Could someone please tell me why I am getting an error. I used the "Record Macro" to capture a few things and then I added some of my own stuff. The macro fails almost immediately. Why is this?

    Also, all the columns in the chart are dynamic ranges.

    here is the macro code. it just starts the creation of a graph.
    Sub Macro()
    '
    ' Test Macro
    '
    
    '
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlLine
        ActiveChart.PlotArea.Select
        Application.CutCopyMode = False
        ActiveChart.SeriesCollection(1).Delete
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(1).Name = "=""Power"""
        ActiveChart.SeriesCollection(1).Values = "==Fordat.xlsm!Power"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(2).Name = "=""Amp Hours"""
        ActiveChart.SeriesCollection(2).Values = "==Fordat.xlsm!Amp_Hours"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(3).Name = "=""Vbatt"""
        ActiveChart.SeriesCollection(3).Values = "==Fordat.xlsm!Vbatt"
        ActiveChart.SeriesCollection(3).XValues = "==Fordat.xlsm!Time"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(4).Name = "=""Ibatt"""
        ActiveChart.SeriesCollection(4).Values = "==Fordat.xlsm!Ibatt"
    End Sub
    Here are pictures of the error. I have also included the excel file.
    error.JPGerror2.JPG

    Thanks,
    Willis
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: VBA Error when running macro

    Two things come to mind.
    1. I do not think you need two equal signs for the values.
    2. Make sure the named range Power exists in your file and it has data.

    If that doesn't help, upload your file so we can help you futher
    Regards,
    Vandan

  3. #3
    Registered User
    Join Date
    07-19-2012
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Error when running macro

    Okay that fixed it. For some reason when I was recording the macro erroneous information was added.

    I have one more problem and then I think everything should be fixed.

    Whenever I generate the graph using the macro, a bunch of "ghost" series appear that I didn't create and mess up my graph.

    Any ideas? I have attached the code and picture.
    Sub Test()
    '
    ' Test Macro
    '
    
    '
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlLine
        Application.CutCopyMode = False
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(1).Name = "Power"
        ActiveChart.SeriesCollection(1).Values = "=KEWL_WT.xlsm!Power"
        ActiveChart.SeriesCollection(1).XValues = "=KEWL_WT.xlsm!Time"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(2).Name = "Amp Hours"
        ActiveChart.SeriesCollection(2).Values = "=KEWL_WT.xlsm!Amp_Hours"
        ActiveChart.SeriesCollection(2).XValues = "=KEWL_WT.xlsm!Time"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(3).Name = "Vbatt"
        ActiveChart.SeriesCollection(3).Values = "=KEWL_WT.xlsm!Vbatt"
        ActiveChart.SeriesCollection(3).XValues = "=KEWL_WT.xlsm!Time"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(4).Name = "Ibatt"
        ActiveChart.SeriesCollection(4).Values = "=KEWL_WT.xlsm!Ibatt"
        ActiveChart.SeriesCollection(4).XValues = "=KEWL_WT.xlsm!Time"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(5).Name = "Vtec"
        ActiveChart.SeriesCollection(5).Values = "=KEWL_WT.xlsm!Vtec"
        ActiveChart.SeriesCollection(5).XValues = "=KEWL_WT.xlsm!Time"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(6).Name = "Ta"
        ActiveChart.SeriesCollection(6).Values = "=KEWL_WT.xlsm!Ta"
        ActiveChart.SeriesCollection(6).XValues = "=KEWL_WT.xlsm!Time"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(7).Name = "Tc"
        ActiveChart.SeriesCollection(7).Values = "=KEWL_WT.xlsm!Tc"
        ActiveChart.SeriesCollection(7).XValues = "=KEWL_WT.xlsm!Time"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(8).Name = "Te"
        ActiveChart.SeriesCollection(8).Values = "=KEWL_WT.xlsm!Te"
        ActiveChart.SeriesCollection(8).XValues = "=KEWL_WT.xlsm!Time"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(9).Name = "Interrupt"
        ActiveChart.SeriesCollection(9).Values = "=KEWL_WT.xlsm!Clock"
        ActiveChart.SeriesCollection(9).XValues = "=KEWL_WT.xlsm!Time"
        ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Graph"
        ActiveChart.ChartArea.Select
        ActiveChart.SeriesCollection(9).Select
        ActiveChart.SeriesCollection(9).AxisGroup = 2
        ActiveChart.ChartArea.Select
        ActiveChart.SeriesCollection(9).Select
        ActiveChart.SeriesCollection(3).Select
        ActiveChart.SeriesCollection(3).AxisGroup = 2
        ActiveChart.ChartArea.Select
        ActiveChart.SeriesCollection(3).Select
        ActiveChart.SeriesCollection(2).Select
        ActiveChart.SeriesCollection(2).AxisGroup = 2
        ActiveChart.ChartArea.Select
        ActiveChart.SeriesCollection(2).Select
        ActiveChart.SeriesCollection(5).Select
        ActiveChart.SeriesCollection(5).AxisGroup = 2
        ActiveChart.ChartArea.Select
        ActiveChart.SeriesCollection(5).Select
        ActiveChart.SeriesCollection(1).Select
        ActiveChart.SeriesCollection(1).AxisGroup = 2
        ActiveChart.ChartArea.Select
        ActiveChart.SeriesCollection(1).Select
        ActiveChart.SeriesCollection(4).Select
        ActiveChart.SeriesCollection(4).AxisGroup = 2
        ActiveChart.ChartArea.Select
        ActiveChart.PlotArea.Select
    End Sub
    error3.jpg

    Thanks in advance

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Error when running macro

    if the current cell has data in it then the current region will be used as the source data for the chart when it is created. if you select an empty cell first you shouldn't see the issue.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Registered User
    Join Date
    07-19-2012
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Error when running macro

    Oh that is fantastic. I just added a line that selects a empty cell before the chart gets created in my macro. Thanks!

    Now I have one last question. Is there an easy way to have the user input a specific range of data that he wants to plot. Say there are 500 entries. Running the macro will generate a graph that displays the entire range. I then want the user to be able to say "display from 200 to 300", and then the macro will create a zoomed graph on a seperate page of that range.

    Thanks for all the help so far.

  6. #6
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: VBA Error when running macro

    http://www.oaltd.co.uk/Excel/Default.htm

    this is Stephen Bullen's page

    Look for charting example and file called funchart7.zip

    This demonstrates how to used defined names and scroll / zoom bar. Cool stuff.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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