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 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

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.

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

Open in new window

Juan VelasquezAuthor Commented:
I'm creating the chart via automation from within  When I tried the code, stating that Selection was not declared
Juan VelasquezAuthor Commented:
Re "I'm creating the chart via automation from within  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()
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

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

Open in new window

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


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.