+ Reply to Thread
Results 1 to 4 of 4

Need Help with Dynamic Chart Worksheet Change Macro

  1. #1
    Registered User
    Join Date
    12-06-2005
    Posts
    16

    Need Help with Dynamic Chart Worksheet Change Macro

    I have the following code, but can't seem to correct the error generated with the line that sets the source data. Can someone help me out? I have data in columns A and B, but the # of rows vary based on a combobox pulldown value. I would like the chart to update automatically with the # of rows that get populated with data. Thanks in advance for any help.

    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim LastRow As Long
    Dim MyRange As Range

    LastRow = Range("A1").End(xlDown).Row
    Set MyRange = Range("A1:B" & LastRow)

    If Not Intersect(Target, MyRange) Is Nothing Then
    ActiveSheet.ChartObjects("Chart 3").SetSourceData _
    Source:=Sheets("Sheet2").Range(MyRange)
    End If

    End Sub

  2. #2
    Andy Pope
    Guest

    Re: Need Help with Dynamic Chart Worksheet Change Macro

    Hi,

    You need to alter the Source range and you also omitted the .Chart
    object reference.

    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim LastRow As Long
    Dim MyRange As Range

    LastRow = Range("A1").End(xlDown).Row
    Set MyRange = Range("A1:B" & LastRow)

    If Not Intersect(Target, MyRange) Is Nothing Then
    ActiveSheet.ChartObjects("Chart 3").Chart.SetSourceData _
    Source:=MyRange
    End If

    End Sub

    Cheers
    Andy

    clmarquez wrote:
    > I have the following code, but can't seem to correct the error generated
    > with the line that sets the source data. Can someone help me out? I
    > have data in columns A and B, but the # of rows vary based on a
    > combobox pulldown value. I would like the chart to update
    > automatically with the # of rows that get populated with data. Thanks
    > in advance for any help.
    >
    > Private Sub Worksheet_Change(ByVal Target As Range)
    >
    > Dim LastRow As Long
    > Dim MyRange As Range
    >
    > LastRow = Range("A1").End(xlDown).Row
    > Set MyRange = Range("A1:B" & LastRow)
    >
    > If Not Intersect(Target, MyRange) Is Nothing Then
    > ActiveSheet.ChartObjects("Chart 3").SetSourceData _
    > Source:=Sheets("Sheet2").Range(MyRange)
    > End If
    >
    > End Sub
    >
    >


    --

    Andy Pope, Microsoft MVP - Excel
    http://www.andypope.info

  3. #3
    Registered User
    Join Date
    12-06-2005
    Posts
    16
    Thanks Andy.

  4. #4
    Andrew Taylor
    Guest

    Re: Need Help with Dynamic Chart Worksheet Change Macro

    It's possible to create dynamic charts without using macros. See
    for example:

    http://www.tushar-mehta.com/excel/ne...ynamic_charts/



    clmarquez wrote:
    > I have the following code, but can't seem to correct the error generated
    > with the line that sets the source data. Can someone help me out? I
    > have data in columns A and B, but the # of rows vary based on a
    > combobox pulldown value. I would like the chart to update
    > automatically with the # of rows that get populated with data. Thanks
    > in advance for any help.
    >
    > Private Sub Worksheet_Change(ByVal Target As Range)
    >
    > Dim LastRow As Long
    > Dim MyRange As Range
    >
    > LastRow = Range("A1").End(xlDown).Row
    > Set MyRange = Range("A1:B" & LastRow)
    >
    > If Not Intersect(Target, MyRange) Is Nothing Then
    > ActiveSheet.ChartObjects("Chart 3").SetSourceData _
    > Source:=Sheets("Sheet2").Range(MyRange)
    > End If
    >
    > End Sub
    >
    >
    > --
    > clmarquez
    > ------------------------------------------------------------------------
    > clmarquez's Profile: http://www.excelforum.com/member.php...o&userid=29386
    > View this thread: http://www.excelforum.com/showthread...hreadid=500034



+ 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