+ Reply to Thread
Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    08-10-2009
    Location
    Fort Collins, Colorado
    MS-Off Ver
    Excel 2003
    Posts
    29

    vba to create chart in certain position after data is entered

    Hi, I have a worksheet that I continually add data to. Each time I add a new row of data, I need a new chart to be generated under the previous chart that I added for the previous row's data. Obviously, the new chart will be in different location each time.

    First, how is location specified in VBA when a new chart is created?

    And secondly, how would code be written to locate the previous chart and place a new chart underneath it for each new row of data added?

    Any suggestions would be greatly appreciated.

    Thanks,
    Buck
    Last edited by buckfran; 08-10-2009 at 02:17 PM. Reason: bad subject

  2. #2
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,213

    Re: Special chart creation

    Hello Buck,

    Please edit your post and amend your thread title to something descriptive of your problem. "Special chart creation" isn't accurate. Perhaps something like "vba to create chart in certain position after data is entered".

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: vba to create chart in certain position after data is entered

    If you are using code to add the chart you can specify it's size on insertion.

    This will get the position and size of the most recently added chartobject
    Code:
    with activesheet.chartobjects(activesheet.chartobjects.count)
       debug.print .left,.top,.width,.height
    end with
    Cheers
    Andy
    www.andypope.info

  4. #4
    Registered User
    Join Date
    08-10-2009
    Location
    Fort Collins, Colorado
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: vba to create chart in certain position after data is entered

    That helps but my main question involves automatic new chart creation. I am hoping to not have to manually insert a new chart each time. Rather, I would like VBA to take the new data I enter on another worksheet and use it to create a new chart underneath the previous new chart. The goal is automation if that's possible.

    Thanks

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: vba to create chart in certain position after data is entered

    There are plenty of examples on the forum of inserting, sizing and populating a chart via code. There is also the macro recorder.

    That snippet was just the information required to determine where the last one was placed.

    If you post an example workbook and explain how the code knows when to add a chart I'm sure we can help progress your problem.
    Cheers
    Andy
    www.andypope.info

  6. #6
    Registered User
    Join Date
    08-10-2009
    Location
    Fort Collins, Colorado
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: vba to create chart in certain position after data is entered

    Yes, that does indeed help me but what I'd really like to do is be able to use that information and not just view it in the Immediate window.

    Thanks

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: vba to create chart in certain position after data is entered

    If you post an example workbook and explain how the code knows when to add a chart I'm sure we can help progress your problem.
    Cheers
    Andy
    www.andypope.info

  8. #8
    Registered User
    Join Date
    08-10-2009
    Location
    Fort Collins, Colorado
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: vba to create chart in certain position after data is entered

    I think I have the design of the code figured out. What would be nice is if there was a way to count the number of charts on a page, and store that value.

    Is that possible?

    Also is it possible to define the chart's location with a variable rather than a value in this code?

    Code:
    Set myChtObj = Worksheets("Individual Jobs").ChartObjects.Add _
                        (Left:=12, Width:=370, Top:=2042, Height:=239)
    Thanks for your time
    Last edited by buckfran; 08-14-2009 at 11:27 AM.

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: vba to create chart in certain position after data is entered

    there is a way to count the charts, I posted it before.

    Code:
    activesheet.chartobjects.count
    Try building on this
    Code:
    Sub AddChart()
        
        Dim objLast As ChartObject
        Dim objNew As ChartObject
        
        If ActiveSheet.ChartObjects.Count = 0 Then
            Set objNew = ActiveSheet.ChartObjects.Add(10, _
                                                      10, _
                                                      400, _
                                                      300)
        Else
            Set objLast = ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
            Set objNew = ActiveSheet.ChartObjects.Add(objLast.Left, _
                                                       objLast.Top + objLast.Height, _
                                                       objLast.Width, _
                                                       objLast.Height)
        End If
                                                       
    End Sub
    Cheers
    Andy
    www.andypope.info

  10. #10
    Registered User
    Join Date
    08-10-2009
    Location
    Fort Collins, Colorado
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: vba to create chart in certain position after data is entered

    and how would I change the syntax if I was referring to a specific other sheet rather than the active sheet.

    Sorry, I'm rather new to VBA

    I assume I need a with block?

    Thanks

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: vba to create chart in certain position after data is entered

    Code:
     Worksheets("Sheet1").ChartObjects.Count
    Cheers
    Andy
    www.andypope.info

  12. #12
    Registered User
    Join Date
    08-10-2009
    Location
    Fort Collins, Colorado
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: vba to create chart in certain position after data is entered

    Thanks, that helps. I am now receiving an "object required" error on that line of code (with the 3 asterisks). Do you know why this would be?

    Code:
    Dim myChtObj As ChartObject, objLast As ChartObject
        
    Set objLast = Worksheets("Graph Reference").ChartObjects.Count ***
    Set myChtObj = Worksheets("Graph Reference").ChartObjects.Add(objLast.Left, _
                                                       objLast.Top + 500, _
                                                       objLast.Width, _
                                                       objLast.Height)
    Thanks for your help.

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: vba to create chart in certain position after data is entered

    because for some reason your are trying to set a reference to the Number of chart objects.
    Cheers
    Andy
    www.andypope.info

  14. #14
    Registered User
    Join Date
    08-10-2009
    Location
    Fort Collins, Colorado
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: vba to create chart in certain position after data is entered

    Code:
    Set objLast = ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
    I was trying, albeit doing a bad job, to modify this line that you suggested earlier so that it would apply to a specific sheet. Should it read like this for my purposes?

    Code:
    Set objLast = Worksheets("Graph Reference").ChartObjects(Worksheets("Graph Reference").ChartObjects.Count)
    Thanks for your time, I'm new to this.

  15. #15
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: vba to create chart in certain position after data is entered

    If you use the WITH statement you need only type the sheet reference once.

    Code:
    with Worksheets("Graph Reference")
        Set objLast = .ChartObjects(.ChartObjects.Count)
    end with
    Cheers
    Andy
    www.andypope.info

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.2.0