Consulting

Results 1 to 4 of 4

Thread: Dynamic Ranges Chart VBA

  1. #1
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location

    Dynamic Ranges Chart VBA

    I need some help. I'm trying to do a dynamic range in a source box in a chart. I have a simple macro recording to create 3 charts every morning.

    Is there a code that it will dynamically select a range from cell F20 to the last active cell in that column for the values box of the chart and select a range from cell G20 to the last active cell of that column for the Category Label box? I know there is a named ranges solution but it wouldn’t work out in my situation, long story.

    Here is a macro I recorded. Can it be modified to do that?

    [vba]Sheets("1st Shift").Select
    ActiveSheet.ChartObjects("Chart 37").Activate
    ActiveChart.PlotArea.Select
    ActiveChart.SeriesCollection(1).XValues = "=Table!R20C7:R21C7" 'F20:f Vaules:
    ActiveChart.SeriesCollection(1).Values = "=Table!R20C6:R21C6" 'G20:g Category Labels:
    ActiveWindow.Visible = False
    Windows("Copy of MS Performance Chartss.xls").Activate
    [/vba]
    Last edited by Shazam; 04-07-2006 at 07:44 PM.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Can you clean up a copy of it and post it? Why do you say a named range solution wouldn't work?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3

  4. #4
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by lucas
    Can you clean up a copy of it and post it? Why do you say a named range solution wouldn't work?

    Hi Lucas thank you for replying. The reason I don?t use name ranges in this situation because I get a REF!# errors in the Define Name Box when I copy the worksheet to another workbook. I use a code to consolidate various workbooks into one master workbook and allot of these workbooks contains charts and when using name ranges I'm getting errors in the Define Name Box.

    Well I finally figured it out how to get this to work of course I'm not a master in VBA it took me awhile but here?s what I got and it seems to work. I left a small sample file below with a macro named "test". Please let me know if I left something out.


     
     Sub test()
    'Choose the worksheet that contains the chart
        Sheets("1st Shift").Select
     
        'Excel names your chart so to be sure do a macro recording
        'and select the chart and stop recording then look at the
        'recording and it will tell you the name of your chart
        ActiveSheet.ChartObjects("Chart 37").Activate
     
        'Define your ranges in the source box of your charts
        ActiveChart.SeriesCollection(1).Select
        ActiveChart.SeriesCollection(1).Values = Range(Sheets("Table").[F20], Sheets("Table").[F65536].End(xlUp)) ' Values:
        ActiveChart.SeriesCollection(1).XValues = Range(Sheets("Table").[E20], Sheets("Table").[E65536].End(xlUp)) 'Category Lables:
     
        'Deselect your chart
        ActiveWindow.Visible = False
     
        'Name of your workbook
        Windows("Sample Test.xls").Activate
        End Sub
    Thank You for the link Justinlabenne.
    Last edited by Shazam; 04-07-2006 at 09:24 PM.

Posting Permissions

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