+ Reply to Thread
Results 1 to 4 of 4

Loop through cells and add to union(range)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-14-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    186

    Loop through cells and add to union(range)

    I have these values in my excel spreadsheet from A1:C6

    1 6 1
    2 2 6
    3 34 2
    4 5 4
    5 1 6
    6 5 5


    I am using the vba code below to select all cells with 6:

    Sub test()
    
    Dim rng As Range
    Set rng = Range("A1:C6")
    
    Dim newRng As Range
    
    
    For Each cell In rng
        cell.Select
        If cell.Value = 6 Then
            If Not newRng Is Nothing Then
                newRng = Union(newRng, Range(cell.Address))
            Else
                Set newRng = Range(cell.Address)
            End If
        End If
    Next cell
    
    newRng.Select
    
    
    End Sub

    but newRng.select is only selecting one cell and that is B1.
    What am I doing wrong?

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Loop through cells and add to union(range)

    Hi,

    You missed a Set
            If Not newRng Is Nothing Then
                Set newRng = Union(newRng, Range(cell.Address))
            Else
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,924

    Re: Loop through cells and add to union(range)

    When you have more than 1 column in range that you are looping, you should specify .Cells object of range.

    For Each cell in rng.Cells
    Also since you declared newRng as range object, you should Set newRng instead of just newRng = xxxx.

    So your code becomes...

    Sub test()
    Dim newRng As Range
    Dim rng As Range
    Dim cell As Range
    
    Set rng = Range("A1:C6")
    
    For Each cell In rng.Cells
        cell.Select
        If cell.Value = 6 Then
            If Not newRng Is Nothing Then
                Set newRng = Union(newRng, Range(cell.Address))
            Else
                Set newRng = Range(cell.Address)
            End If
        End If
    Next cell
    
    newRng.Select
    
    End Sub
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  4. #4
    Forum Contributor
    Join Date
    06-14-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    186

    Re: Loop through cells and add to union(range)

    thank you - its working now

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Opposite of range.union?
    By radddogg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-20-2017, 07:36 AM
  2. [SOLVED] Not getting how to write a Union Range
    By capson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-22-2014, 09:54 AM
  3. Union Range Problem
    By LDwoods in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2014, 09:45 PM
  4. [SOLVED] Range/union
    By Dibbley247 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-22-2013, 10:35 AM
  5. How to loop with union() / select multiple ranges at ones
    By furiousfox in forum Excel General
    Replies: 6
    Last Post: 06-02-2010, 08:02 AM
  6. [SOLVED] union range problem
    By Walter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2006, 07:35 PM
  7. Union/Range/Cells
    By KentÄ in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-11-2005, 08:06 AM

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