+ Reply to Thread
Results 1 to 3 of 3

Chart axis labels not updating properly with VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    11-26-2008
    Location
    Michigan, USA
    Posts
    18

    Chart axis labels not updating properly with VBA

    Hello,

    I am working on a workbook where an on-page chart has to be updated depending on what the user wants to see displayed. I've attached an example of this workbook. The user would click one of the colored boxes on the left side of the page, and the means for that component, as well as means from previous periods of time would be displayed on the chart, along with the labels as to which variable they correspond with. The data and labels are being read from the second sheet in the workbook.

    The data is being properly updated, but the labels do not change. If I check the data being read for the chart, it appears to be pulling over the correct labels, but unless I manually confirm the new labels, they are not displayed. The macro I am using works perfectly in Excel 2003, and was working just fine in 2007 until I copied the sheets (which I will need to do for the project I am working on).

    The code is this:

    Dim LabelRange As Range                 'Range to hold labels for the chart
    Dim CurrentRange As Range               'Range to hold current scores of questions
    Dim OldRange1 As Range             'Range to hold old scores of questions (first time period back)
    Dim OldRange2 As Range             'Range to hold old scores of questions (second time period back)
    
    Sub ChangeQuestionScoreGraph()
        ScoreSheet = ActiveSheet.Name
        DataSheet = ActiveSheet.Next.Name
        NamesSheet = ActiveSheet.Next.Next.Name
        
        Application.ScreenUpdating = False
        
        CompName = Application.Caller
        
        Sheets(DataSheet).Activate
        Range("A1").Select
        Cells.Find(What:=CompName, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            True, SearchFormat:=False).Activate
        
        FirstRow = ActiveCell.Row
        If ActiveCell.Offset(1) <> "" Then
            Selection.End(xlDown).Select
        End If
        LastRow = ActiveCell.Row
        
        Set LabelRange = Range("B" & FirstRow & ":B" & LastRow)
        Set CurrentRange = Range("H" & FirstRow & ":H" & LastRow)
        Set OldRange1 = Range("N" & FirstRow & ":N" & LastRow)
        Set OldRange2 = Range("O" & FirstRow & ":O" & LastRow)
        
        Sheets(NamesSheet).Activate
        Range("A1").Select
        Cells.Find(What:=CompName, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            True, SearchFormat:=False).Activate
            
        ThisChartTitle = ActiveCell.Offset(, 1)
        
        Sheets(ScoreSheet).Activate
        
        ActiveSheet.ChartObjects("ScoreChart1").Activate
        ActiveChart.SeriesCollection(1).XValues = LabelRange
        ActiveChart.SeriesCollection(1).Values = OldRange2
        ActiveChart.SeriesCollection(2).Values = OldRange1
        ActiveChart.SeriesCollection(3).Values = CurrentRange
        
        ActiveChart.ChartTitle.Text = ThisChartTitle
        ActiveChart.Refresh
        
        Range("A1").Select
        
        Application.ScreenUpdating = True
    End Sub

    Is there any way to force an update of the labels? I would think .Refresh would do the trick, but no such luck.

    Thank you for your help.
    Attached Files Attached Files
    Last edited by d3hartm2; 01-05-2009 at 12:09 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    xl2007 requires you to set the labels for all the series.

        ActiveSheet.ChartObjects("ScoreChart1").Activate
        ActiveChart.SeriesCollection(1).XValues = LabelRange
        ActiveChart.SeriesCollection(1).Values = OldRange2
        ActiveChart.SeriesCollection(2).XValues = LabelRange
        ActiveChart.SeriesCollection(2).Values = OldRange1
        ActiveChart.SeriesCollection(3).XValues = LabelRange
        ActiveChart.SeriesCollection(3).Values = CurrentRange
    In xl2003 you would get away with just setting the first series.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    11-26-2008
    Location
    Michigan, USA
    Posts
    18
    Andy,

    Thank you very much, that worked perfectly!

+ 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