Setting XValues to a dynamic range

I have the following code that sets the x axis so that the x axis is set to the years located in "=Sheet1!$F$1:$N$1"
.SeriesCollection(1).XValues = "=Sheet1!$F$1:$N$1"
Now the range of years will always be in the first row (header row) and will start cell F1, however the number of years will be dynamic.  So I need to set .SeriesCollection(1).XValues to a dynamic range.  I am using automation from within vb.net to create the graph. in an external Excel spreadsheet.

Juan VelasquezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gtglonerCommented:
Try this code snippet:
Range("F1").Select
Range(Selection, Selection.End(xlToRight)).Select

Open in new window

0
Juan VelasquezAuthor Commented:
I'm creating the chart via automation from within vb.net.  When I tried the code, stating that Selection was not declared
0
Juan VelasquezAuthor Commented:
Re "I'm creating the chart via automation from within vb.net.  When I tried the code, stating that Selection was not declared"  Never mind I took care of it via
 chartRange(excelApp.Selection, excelApp.Selection.End(excelApp.xlToRight)).Select()
0
Juan VelasquezAuthor Commented:
No it doesn't work.  I get the following error -Conversion from string "F1" to type 'Integer' is not valid. on line excelApp.Range(excelRange("F1")).Select()
At this point excelRange is set to A1:N7


excelApp.Range(excelRange("F1")).Select()
                    Dim rng As Excel.Range = excelApp.Range(excelApp.Selection, excelApp.Selection.End(excelApp.xlToRight)).Select()
                    .SeriesCollection(1).XValues = rng

Open in new window

0
Juan VelasquezAuthor Commented:
Here is the code that I came up with and so far it seems to work well
 If xlDataTable.Columns.Count > colCharsetLen Then
                            finalColLetter = colCharset.Substring( _
                             (xlDataTable.Columns.Count - 1) \ colCharsetLen - 1, 1)
                        End If
                        finalColLetter += colCharset.Substring( _
                      (xlDataTable.Columns.Count - 1 + intH) Mod colCharsetLen, 1)
                        excelRange = String.Format("A1:{0}{1}", finalColLetter, xlDataTable.Rows.Count + 1)
                       
                        '****The following code is used to create variables that will be used in the Create Graph section****
                        'Creates an array to hold the upper and lower limits of the range of the excel table
                        Dim strChartRange As String() = excelRange.Split(CChar(":"))
                        'Stores the first cell in the range
                        strFirstCell = strChartRange(0)
                        'Stores the last cell in the range
                        strLastCell = strChartRange(1)

                        'MessageBox.Show(Mid(strLastCell, 1, Len(strLastCell) - 1).ToString)
                        .SeriesCollection(1).XValues = "=Sheet1!$K$1:$" & Mid(strLastCell, 1, Len(strLastCell) - 1) & "$1"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.