Results 1 to 6 of 6

Thread: Expanding Excel Ranges

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Expanding Excel Ranges

    How does one go about expanding a defined Excel Range? I have it defined as:
    Code:
    Dim TempRange As Excel.Range = Globals.Sheet1.Range("E4:P4")
    Now what I want to do is add items into that range. In actuality, I need to add "P4" at the beginning, and "E4" at the end of TempRange, keeping all of the items in the original TempRange as well (two items on top of the total cells in TempRange). Is there an easy way to assign an array of values and put it back into an Excel.Range object?

    Using VS 2008, Excel 2007, VSTO.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Expanding Excel Ranges

    "E4:P4" are 12 items... I want a range of 14 items, with P4 at the beginning and E4 at the end... with the original 12 items in between.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Expanding Excel Ranges

    Well when I try that, it still doesn't do what is intended.
    Code:
    Dim TempRange As Excel.Range = Globals.Sheet1.Range("P4,E4:P4,E4")
    The E4:P4 range actually gets plotted first, then P4 and E4.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Expanding Excel Ranges

    Yes, it does show 14 items, but the problem is that I am trying to chart these values. When you try to add that range to a series, it does show the 14 values, but the "E4:P4" selection is always plotted first, even though "P4" was first in that selection string.
    Code:
            Dim TempRange As Excel.Range = Globals.Sheet1.Range("P4,E4:P4,E4")
            Dim MySeriesColl As Excel.SeriesCollection = MyChart.SeriesCollection
            Dim TempSeries As Excel.Series = MySeriesColl.Add(TempRange)
            TempSeries.Name = "Temperature"
            TempSeries.AxisGroup = Excel.XlAxisGroup.xlPrimary
    No matter what you put for the range, the "E4:P4" part is plotted first, with the single "P4" and "E4" values at the end. I want them in the order specified, and it doesn't want to work grrrrrrrr

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Expanding Excel Ranges

    you can use join to combine ranges
    something like
    set myrange = join(myrange, range("f7:g99"))
    for this example myrange must be an existing range
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Expanding Excel Ranges

    Use different method and syntax that may help:
    Code:
    Dim MySeriesColl As Excel.SeriesCollection = MyChart.SeriesCollection
    Dim TempSeries As Excel.Series = MySeriesColl.NewSeries
    TempSeries.Name = "Temperature"
    TempSeries.Values = "=(Globals.Sheet1!R4C16,Globals.Sheet1!R4C5:R4C16,Globals.Sheet1!R4C5)"
    TempSeries.AxisGroup = Excel.XlAxisGroup.xlPrimary
    R4C16 is "P4", R4C5:R4C16 is "E4:P4", R4C5 is "E4"
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

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



Click Here to Expand Forum to Full Width