Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

(part2) Need help with dynamic XL Chart range 1

Status
Not open for further replies.

JBG

Programmer
Oct 22, 2001
99
0
0
US
Hi experts,

This is part 2 of my dilemma (and I apologize for the long post). I had previously posted that the solution(s) I was given worked, but they did not.

I am trying to update the 5 series in an existing chart in an XL spreadsheet.

Internally (when I look in the excel's VB editor), the chart is referred to as "Chart2"

Here is the snippet of code that does not error, but does not work either:
Code:
.....etc

Dim objExcelApp As Object
Dim objExcelSheet As Object
Dim objExcelWorkbook As Excel.Workbook
Dim objChart As New Excel.Chart
Dim objSeries As Excel.Series
Dim objSC As Excel.SeriesCollection

.....etc

Set objExcelApp = CreateObject("Excel.Application")
Set objExcelWorkbook = objExcelApp.Workbooks.Open(g_strXLpath)
    
Set objChart = objExcelWorkbook.Charts(1)
objChart.Activate
For Each objSeries In objChart.SeriesCollection
            Select Case objSeries.Name

                Case "nbrSmsAvTotalCompPct"
                   
                    objSeries.Values = Worksheets("data").Range("I2:I" & intNumberOfRows + 2)
                Case "nbrSmsAvDTCompPct"
                   
                    objSeries.Values = Worksheets("data").Range("M2:M" & intNumberOfRows + 2)
                Case "nbrSmsAvLTCompPct"

                    
                    objSeries.Values = Worksheets("data").Range("Q2:Q" & intNumberOfRows + 2)
                Case "nbrSmsAvTotalUnver"
                    
                    objSeries.Values = Worksheets("data").Range("R2:R" & intNumberOfRows + 2)
                Case "nbrSmsAvDTUnver"
                    
                    objSeries.Values = Worksheets("data").Range("S2:S" & intNumberOfRows + 2)
                Case "nbrSmsAvLTUnver"
                    
'                   'objSeries.Values = Worksheets("data").Range("T2:T" & intNumberOfRows + 2)
            End Select
        Next

The deal here is that, when you right click on the  chart and go to Source Data, you get a 2-tab window that defines the data ranges for each "series" that the chart utlizes. 

Now, the chart uses sheet4/"data" (which is 50 cols wide)as its data source. The chart uses only about 25 of those cols, and each time I add a row to sheet4 (which I can do no problem) I need to adust the data ranges (within the chart) that the chart uses so that it utilizes/recognizes the new row.

For example, the first series, which I will call nbrSmsAvTotalCompPct, uses this as the "Name" (lifted directly from the Source Data 2-tab window, tab 2): =Data!$I$1

and this as the "Values": "=Data!$I$2:$I$10"

and this as the "Xvalues": "=Data!$B$2:$B$10"

What I would like is some sample VBA code (that I will use in an Access module) to be able to update , I think/hope, the "Values" so that the chart will now use/recognize the new row that has been added.

So, I want "=Data!$I$2:$I$10" 

to change to

"=Data!$I$2:$I$11"


In a previous reply by Office Solutions.com, he posted a a cool fix used in the spreadsheet, which I couldnt get to work. But I want to be able to program this for several reasons.

So, anyone have some sample code and a quick explanation as to which objects are used ?(I do not even know if the above code is on the right track)

I am snorkling around the solution using setDataSource, ChartWizard, and Series, but cannot get this dang thing to work. I am past the frustrated stage and need a bit more help before I, well, snap.

Again.... HELP!

Thanks,

Jeff
 
Have you tried this after Series change ?
objChart.Refresh

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I tried it, but it still didnt work.

Here is what the "values" looks like after the code runs:

={0.95317161758983,0.995208674820325,0.983090984628168,0.975676242398826,0.975963756868996,0.975963756868996,0.975963756868996}

Ouch.
 
Jeff

If you DEFINE your ranges in Insert/Name/Define using eh OFFSET formula (as I recommended in your former post) you would not be having this problem.

1. you define each range with a unique name in Insert/Name/Define

2. you use the defined name in the Series Data Source. For instance if Sheet1 has range name "rDates" defined for the category series then the Category Textbox would be changed to...
Code:
=Sheet1!rDates
That's ALL that is necessary to define and use a range in a chart!!!

NO VBA NECESSARY...

unless you want to switch to a different data source!!!

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hi Skip,

I think I get the beauty of your method....

Once I define the cells with a name that will take into acct. however many rows are entered, I take that defined name and use it as the series value....

OK, I am going to try it Monday at work. I may have a question or 2 along the way...

Thank you again for the posts..

Jeff
 
OK Skip,here are the questions.

The spreadsheet I have is about 50 columns wide with 10 rows of data in it already....about 28 of the columns use formulae to derive their data from other columns, whilst the other columns are just data.

The fix for my issues is:

=OFFSET(Sheet1!$A$2,0,0,CountA(Sheet1!$A:$A)-1,1)

Correct?

Now, which cells, of which columns, do I enter this into (using the define/name, etc)?

Do I put this formula into each first cell of each column? Or, since there is already 10 rows of data, do I put this formula in each of the last (the 10th) cells for each column?

Jeff
 
Jeff,

=OFFSET(Sheet1!$A$2,0,0,CountA(Sheet1!$A:$A)-1,1)

defines the range of DATA (sans heading) in column A. When you define this range in Insert/Name/Define, you associate this range with a name -- I'll pretend it's "rCategory"

=OFFSET(Sheet1!$B$2,0,0,CountA(Sheet1!$A:$A)-1,1)

would define the range in column B. Let's say it's name is "rAmount"

You do not change anything on the sheet!

In the Chart/Source Data/Series you will use these names instead of the ABSOLUTE references for each series like...
Code:
=Sheet1!rCategory
:)

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Two things.

One.

Thanks for letting me beat this thing into the ground with you.

Two.

I will try in in just a few minutes.

Three ( I lied above)...

Thanks!

JEff

 
Props to you Skip. Worked like a charm, and of course, now that my project lead and I did it, it is sooooo easy.

A cyber high-5 to you from me and Tim, the project lead.

Thanks a jillion,

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top