Consulting

Results 1 to 3 of 3

Thread: Chart Range References

  1. #1

    Chart Range References

    For reference I have a chart on Sheet1 (Overview) that is supposed to reference data on Sheet2 (Sheet2).

    [vba]Dim months As Range
    Dim first_month_num As Integer
    Dim last_month_num As Integer

    Set months = ThisWorkbook.Sheets("Sheet2").Range( _
    ThisWorkbook.Sheets("Sheet2").Cells(last_month_num + 2, 1), _
    ThisWorkbook.Sheets("Sheet2").Cells(first_month_num + 2, 1) _
    )

    With ThisWorkbook.Sheets("Overview").ChartObjects("Chart 10").Chart
    With .SeriesCollection(1)
    .Name = "Program Views (Month)"
    .values = ThisWorkbook.Sheets("Sheet2").Range( _
    ThisWorkbook.Sheets("Sheet2").Cells(last_month_num + 2, 2), _
    ThisWorkbook.Sheets("Sheet2").Cells(first_month_num + 2, 2) _
    ).Value
    .XValues = months
    End With
    With .SeriesCollection(2)
    .Name = "Program Views (PTD)"
    .values = ThisWorkbook.Sheets("Sheet2").Range( _
    ThisWorkbook.Sheets("Sheet2").Cells(last_month_num + 2, 3), _
    ThisWorkbook.Sheets("Sheet2").Cells(first_month_num + 2, 3) _
    ).Value
    .XValues = months
    End With
    End With[/vba]

    When I run this, I get "A formula in this worksheet contains one or more invalid references", and the script stops. In Excel 2003 I can step through this line-by-line and everything is fine. In Excel 2007 I get an Automation error and an Unspecified error when I try to step through. I can tell that it doesn't like the way I am referencing my ranges, but I don't know how to fix it. I am using this style of reference in a couple other places, and they seem to be the only sticking points in my code.

  2. #2

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings,
    I see that you tried twice, to no avail thus far. I do not have Exel 2007, and rarely use charts, so this is more at what might help you get an answer.

    [VBA]Set months = ThisWorkbook.Sheets("Sheet2").Range( _
    ThisWorkbook.Sheets("Sheet2").Cells(last_month_num + 2, 1), _
    ThisWorkbook.Sheets("Sheet2").Cells(first_month_num + 2, 1) _
    )[/VBA]

    As is appears from the snippet that both "first_..." and "last_..." are empty, which is probably not true... How about including a sample workbook where the error might be discernable?

Posting Permissions

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