+ Reply to Thread
Results 1 to 8 of 8

A range variable consisting of multiple ranges.

  1. #1
    cpeters5@yahoo.com
    Guest

    A range variable consisting of multiple ranges.

    Is it possible to do the following

    - There are two names in a sheet:
    test1 = "A1:A3" and test2 = "D12:D16"

    - I would like to define a single range variable:
    Dim testing As Range
    Set testing = Union
    (ThisWorkbook.Names"test1").RefersToRange,
    ThisWorkbook.Names("test2").RefersToRange)

    I know that this does not work, but is there a way to fix it ?
    Thanks,
    pac


  2. #2
    Tom Ogilvy
    Guest

    Re: A range variable consisting of multiple ranges.

    Dim testing As Range
    Set testing = Union(Range("Test1"),Range("Test2"))
    Testing.Select

    Test1 and test2 must be on the same sheet.

    (although Your original should work as well. )

    --
    Regards,
    Tom Ogilvy





    <cpeters5@yahoo.com> wrote in message
    news:1104763361.261841.108260@f14g2000cwb.googlegroups.com...
    > Is it possible to do the following
    >
    > - There are two names in a sheet:
    > test1 = "A1:A3" and test2 = "D12:D16"
    >
    > - I would like to define a single range variable:
    > Dim testing As Range
    > Set testing = Union
    > (ThisWorkbook.Names"test1").RefersToRange,
    > ThisWorkbook.Names("test2").RefersToRange)
    >
    > I know that this does not work, but is there a way to fix it ?
    > Thanks,
    > pac
    >




  3. #3
    Bernie Deitrick
    Guest

    Re: A range variable consisting of multiple ranges.

    pac,

    If you used named ranges:

    Dim testing As Range
    Set testing = Union(Range("test1"), Range("test2"))
    MsgBox testing.Address

    HTH,
    Bernie
    MS Excel MVP

    <cpeters5@yahoo.com> wrote in message
    news:1104763361.261841.108260@f14g2000cwb.googlegroups.com...
    > Is it possible to do the following
    >
    > - There are two names in a sheet:
    > test1 = "A1:A3" and test2 = "D12:D16"
    >
    > - I would like to define a single range variable:
    > Dim testing As Range
    > Set testing = Union
    > (ThisWorkbook.Names"test1").RefersToRange,
    > ThisWorkbook.Names("test2").RefersToRange)
    >
    > I know that this does not work, but is there a way to fix it ?
    > Thanks,
    > pac
    >




  4. #4
    cpeters5@yahoo.com
    Guest

    Re: A range variable consisting of multiple ranges.

    Thanks both Bernie and Tom for the answer. It gave me a jump start for
    the day. made a lot of progress.

    However, I was trying to ad a little bit of bells and whistles to the
    code but was stuck with relative position from the range.


    More explicitly, if given

    Test1 = union(range("A1:a10"),range("Q5:Q23"))

    How do I refer to the cell P5 using relative position to Test1?
    Thanks,
    pac


  5. #5
    Dave Peterson
    Guest

    Re: A range variable consisting of multiple ranges.

    You could also use:

    set test1 = range("a1:a10,q5:q23")
    (if you were working with addresses.)

    I'd use something like:

    Dim test1 As Range
    Dim test2 As Range
    Set test1 = Range("a1:a10,q5:q23")
    Set test2 = test1.Cells(1, 1).Offset(4, 15)
    MsgBox test2.Address



    "cpeters5@yahoo.com" wrote:
    >
    > Thanks both Bernie and Tom for the answer. It gave me a jump start for
    > the day. made a lot of progress.
    >
    > However, I was trying to ad a little bit of bells and whistles to the
    > code but was stuck with relative position from the range.
    >
    > More explicitly, if given
    >
    > Test1 = union(range("A1:a10"),range("Q5:Q23"))
    >
    > How do I refer to the cell P5 using relative position to Test1?
    > Thanks,
    > pac


    --

    Dave Peterson

  6. #6
    Tom Ogilvy
    Guest

    Re: A range variable consisting of multiple ranges.

    demo from the immediate window:
    set Test1 = union(range("A1:a10"),range("Q5:Q23"))
    ? test1.Address
    $A$1:$A$10,$Q$5:$Q$23
    ? test1(5,16).Address
    $P$5

    Notice that to make test1 refer to the union of two ranges, you must use SET

    Cells Relative to a range are relative to the upper left corner of the first
    range in the union.

    But, when building a union, order can be important. If you constructed the
    union in reverse you would get a totally different result from the relative
    reference:
    set Test2 = union(range("Q5:Q23"),range("A1:a10"))
    ? Test2.Address
    $Q$5:$Q$23,$A$1:$A$10
    ? Test2(5,16).Address
    $AF$9

    to get P5 you would have to use a different relative value

    ? Test2(1,0).address
    $P$5

    So trying to do a relative reference inside a discontiguous union doesn't
    make much sense to me.

    Dave Peterson was being more specific in that Test1(1,1) identifies a
    single cell, A1 (upper left corner of the first range in the union). He
    then uses the offset function which is zero based to identify P5 relative to
    A1. This is a little redundant, but clearer as to intent. It would
    produce the same wrong answer if used with Test2

    ? test2(1,1).offset(4,15).Address
    $AF$9

    So overtly refering to the upper left corner does not solve the problem with
    the order of the ranges in the union.

    Summarize

    Offset is zero based. Range("A1").offset(0,0) is A1
    the shortcut method is one based. Range("A1")(1,1) is A1

    both have their arguments as row offset, then column offset

    --
    Regards,
    Tom Ogilvy

    <cpeters5@yahoo.com> wrote in message
    news:1104804084.736860.248360@z14g2000cwz.googlegroups.com...
    > Thanks both Bernie and Tom for the answer. It gave me a jump start for
    > the day. made a lot of progress.
    >
    > However, I was trying to ad a little bit of bells and whistles to the
    > code but was stuck with relative position from the range.
    >
    >
    > More explicitly, if given
    >
    > Test1 = union(range("A1:a10"),range("Q5:Q23"))
    >
    > How do I refer to the cell P5 using relative position to Test1?
    > Thanks,
    > pac
    >




  7. #7
    cpeters5@yahoo.com
    Guest

    Re: A range variable consisting of multiple ranges.

    Thanks Tom,

    Insightful lesson!
    If I understand you correctly, relative references on a union of ranges
    will not work if the position these ranges (in relative to each other)
    changes.

    E.g. if column B to P are deleted, the two ranges become

    "A1:A10" and "B5,B23"

    and the reference identification, or offset will refer to a wrong cell,
    or in this example, invalid.

    If this is so and there is no way around it, I will have to change my
    approach and not using union.

    Thanks,
    pac


  8. #8
    Tom Ogilvy
    Guest

    Re: A range variable consisting of multiple ranges.

    When you introduce deleting a column, you open a whole new can of worms.

    set rng = Range("A:A,Q:Q")
    ? rng.Address
    $A:$A,$Q:$Q
    ? columns(2).Delete
    True
    ? rng.Address
    $A:$A,$P:$P
    columns(1).Delete
    ? rng.Address
    $O:$O

    Test out some of your ideas in the immediate window and perhaps you will
    find a solution.

    --
    Regards,
    Tom Ogilvy


    <cpeters5@yahoo.com> wrote in message
    news:1104849021.431026.88620@z14g2000cwz.googlegroups.com...
    > Thanks Tom,
    >
    > Insightful lesson!
    > If I understand you correctly, relative references on a union of ranges
    > will not work if the position these ranges (in relative to each other)
    > changes.
    >
    > E.g. if column B to P are deleted, the two ranges become
    >
    > "A1:A10" and "B5,B23"
    >
    > and the reference identification, or offset will refer to a wrong cell,
    > or in this example, invalid.
    >
    > If this is so and there is no way around it, I will have to change my
    > approach and not using union.
    >
    > Thanks,
    > pac
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1