+ Reply to Thread
Results 1 to 8 of 8

How to exclude certain cells from a range?

  1. #1
    Ed
    Guest

    How to exclude certain cells from a range?

    I have a range set to a group of cells (say A1:E5) -call it rngSquare. I've
    iterated through this and picked out say 15 non-contiguous cells to create
    another range - call it rngWork. Now let's throw in ranges for each row of
    the square - rngRow1 - rngRow5.

    I know I can intersect and union these ranges (such as: every cell in
    rngRow1 that is also in rngWork) to narrow down a search-and-select mission.
    But say I want to search every cell in rngWork **except** rngRow1. Do I
    have to loop through all the ranges like:
    Union(Intersect(rngWork, rngRow2), Intersect(rngWork, rngRow3), etc . . .
    or is there an easier way to do something like:
    Intersect(rngWork, Not rngRow1)???

    Ed



  2. #2
    Norman Jones
    Guest

    Re: How to exclude certain cells from a range?

    Hi Ed,

    See:

    http://tinyurl.com/agpz9


    ---
    Regards,
    Norman



    "Ed" <ed_millis@NO_SPAM.yahoo.com> wrote in message
    news:es5DERSFGHA.1124@TK2MSFTNGP10.phx.gbl...
    >I have a range set to a group of cells (say A1:E5) -call it rngSquare.
    >I've
    > iterated through this and picked out say 15 non-contiguous cells to create
    > another range - call it rngWork. Now let's throw in ranges for each row
    > of
    > the square - rngRow1 - rngRow5.
    >
    > I know I can intersect and union these ranges (such as: every cell in
    > rngRow1 that is also in rngWork) to narrow down a search-and-select
    > mission.
    > But say I want to search every cell in rngWork **except** rngRow1. Do I
    > have to loop through all the ranges like:
    > Union(Intersect(rngWork, rngRow2), Intersect(rngWork, rngRow3), etc . . .
    > or is there an easier way to do something like:
    > Intersect(rngWork, Not rngRow1)???
    >
    > Ed
    >
    >




  3. #3
    Ed
    Guest

    Re: How to exclude certain cells from a range?

    Norman:

    From the first post, I felt the deep waters closing over my head!! 8>)
    I gleaned a bit from the first couple of posts - after that, it went far
    beyond me. (Like my teenagers listening to me!) So I think I will take the
    general assumption that yes, it can be done, but not by me at this point,
    and plod my "kludgy" way through this until my understanding grows a bit
    more.

    Thanks for chiming in.
    Ed

    "Norman Jones" <normanjones@whereforartthou.com> wrote in message
    news:O6xNtZSFGHA.644@TK2MSFTNGP09.phx.gbl...
    > Hi Ed,
    >
    > See:
    >
    > http://tinyurl.com/agpz9
    >
    >
    > ---
    > Regards,
    > Norman
    >
    >
    >
    > "Ed" <ed_millis@NO_SPAM.yahoo.com> wrote in message
    > news:es5DERSFGHA.1124@TK2MSFTNGP10.phx.gbl...
    > >I have a range set to a group of cells (say A1:E5) -call it rngSquare.
    > >I've
    > > iterated through this and picked out say 15 non-contiguous cells to

    create
    > > another range - call it rngWork. Now let's throw in ranges for each row
    > > of
    > > the square - rngRow1 - rngRow5.
    > >
    > > I know I can intersect and union these ranges (such as: every cell in
    > > rngRow1 that is also in rngWork) to narrow down a search-and-select
    > > mission.
    > > But say I want to search every cell in rngWork **except** rngRow1. Do I
    > > have to loop through all the ranges like:
    > > Union(Intersect(rngWork, rngRow2), Intersect(rngWork, rngRow3), etc . .

    ..
    > > or is there an easier way to do something like:
    > > Intersect(rngWork, Not rngRow1)???
    > >
    > > Ed
    > >
    > >

    >
    >




  4. #4
    Walt
    Guest

    Re: How to exclude certain cells from a range?

    Hi Ed,

    The following thread also deals with this subject, is shorter, and
    possibly will give you something you feel you can use:

    http://groups.google.com/group/micro...860f54a2f8e7fe

    Walt Weber


  5. #5
    Ed
    Guest

    Re: How to exclude certain cells from a range?

    Thanks, Walt. I'm still not sure about getting into this - it does look
    quite a bit deeper than I imagined. Since I'm dealing with a smaller
    specific situation, I can probably cobble something together to suit my
    needs that wouldn't be a universal fit. In the meantime, as this could very
    well be something I *will* need on a more universal basis, I've now got
    several things to work my way through.

    Cheers!
    Ed

    "Walt" <xlswalt@aol.com> wrote in message
    news:1136829908.363514.127990@g44g2000cwa.googlegroups.com...
    > Hi Ed,
    >
    > The following thread also deals with this subject, is shorter, and
    > possibly will give you something you feel you can use:
    >
    >

    http://groups.google.com/group/micro...860f54a2f8e7fe
    >
    > Walt Weber
    >




  6. #6
    Ed
    Guest

    Re: How to exclude certain cells from a range?

    With excellent help from Bernie Deitrick in response to my post "Can I do
    this with arrays?" (http://tinyurl.com/d2trc), I have found a method that
    works for me.

    I am trying to create a range that is all the cells in rng1 except the cells
    in rng2. I iterated through all the cells of rng1, created a value from the
    Row & Column of the cell, and read it into aryRng1. Repeated with rng2 and
    aryRng2. Then, using Bernie's code, I removed all the values in ayrRng2
    from aryRng1. Then I iterated through the revised aryRng1 and set a range
    to .Cells(Left(aryRng1(x), Right(aryRng1(x)), using Union to add to the
    range.

    I don't know if this will stand up to all tests, or how fast it will be on
    very large ranges (I'm working with less than 100 cells), but it does work
    for me. I've posted the code for your reading pleasure.

    Thank you, Walt, Tom, Norman and Bernie!
    Ed

    '*******************
    Option Base 1

    Sub TestSetRangeExcludingCells()

    Dim aryRng1 As Variant 'cells of large range
    Dim aryRng2 As Variant 'cells to be excluded

    Dim rng1 As Range 'main range
    Dim rng2 As Range 'range to be excluded
    Dim rng3 As Range 'new range formed
    Dim rngCl As Range 'temp range for cells

    Dim x As Long, y As Long, z As Long
    Dim a As Long, b As Long, c As Long

    Dim wkb As Workbook
    Dim wks As Worksheet

    Set wkb = ActiveWorkbook
    Set wks = wkb.Sheets("Sheet1")

    Set rng1 = wks.Range("A1:E5")
    Set rng2 = wks.Range("B1:B5")

    x = rng1.Cells.Count
    y = rng2.Cells.Count

    ' Read cell references from rng1 into array
    ReDim aryRng1(1 To x) As Variant
    c = 0
    For Each rngCl In rng1
    c = c + 1
    aryRng1(c) = rngCl.Row & rngCl.Column
    Next rngCl

    ' Read cell references from rng2 into array
    ReDim aryRng2(1 To y) As Variant
    c = 0
    For Each rngCl In rng2
    c = c + 1
    aryRng2(c) = rngCl.Row & rngCl.Column
    Next rngCl

    ' The following code is from Bernie Deitrick
    ' from the microsoft.public.excel.programming NG.
    ' It removes any value in aryRng2 from aryRng1.
    ' The result is all cell position references in rng1
    ' except those which also reference cells in rng2.
    On Error Resume Next
    For x = 1 To UBound(aryRng2)
    aryRng1(Application.Match(aryRng2(x), aryRng1, False)) = ""
    Next x

    z = UBound(aryRng1)

    For x = UBound(aryRng1) To 1 Step -1
    If aryRng1(x) = "" Then
    z = z - 1
    For y = x To UBound(aryRng1) - 1
    aryRng1(y) = aryRng1(y + 1)
    Next y
    End If
    Next x

    ReDim Preserve aryRng1(1 To z)
    ' Thank you, Bernie!

    ' Now set a range to the values of aryRng1
    a = Left(aryRng1(1), 1)
    b = Right(aryRng1(1), 1)
    Set rng3 = wks.Cells(a, b)

    For x = 1 To UBound(aryRng1)
    a = Left(aryRng1(x), 1)
    b = Right(aryRng1(x), 1)
    Set rng3 = Union(rng3, wks.Cells(a, b))
    Next x

    rng3.Select

    End Sub

    "Walt" <xlswalt@aol.com> wrote in message
    news:1136829908.363514.127990@g44g2000cwa.googlegroups.com...
    > Hi Ed,
    >
    > The following thread also deals with this subject, is shorter, and
    > possibly will give you something you feel you can use:
    >
    >

    http://groups.google.com/group/micro...860f54a2f8e7fe
    >
    > Walt Weber
    >




  7. #7
    Registered User
    Join Date
    12-18-2020
    Location
    Taipei, R.O.C.
    MS-Off Ver
    2017
    Posts
    3

    Re: How to exclude certain cells from a range?

    Check the following code:

    Please Login or Register  to view this content.
    Last edited by FDibbins; 12-18-2020 at 03:28 AM.

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: How to exclude certain cells from a range?

    bbyeh, this thread is over 14 years old, I doubt anyone is still following it.

    Also, please use code tags when posting code (see my footnote on how to) - I have added them for you - this time
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ 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