+ Reply to Thread
Results 1 to 4 of 4

Need help with cells.find and do if looping

Hybrid View

  1. #1
    Registered User
    Join Date
    12-05-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    35

    Need help with cells.find and do if looping

    I have been trying to adapt the following code

    Sub Test()
    Dim C As Range
    With Range("C2:C15")
    
    Set C = Cells.Find("#N/A")
    If Not C Is Nothing Then
            firstAddress = C.Address
            Do
            LR = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row
            Union(Cells(C.Row, 1), Cells(C.Row, 2), Cells(C.Row, 3), Cells(C.Row, 4)).Copy Sheet2.Cells(LR + 1, "A")
            Set C = Cells.FindNext(C)
            Loop While Not C Is Nothing And C.Address <> firstAddress
    End If
    End With
    End Sub
    What I need it to do is look only in column C for cells that are not = to "#N/A"
    currently it finds N/A's in Multiple columns and returns an entry for each.

    I realize the union code is not required for this example but it will be for other iterations of the macro
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Need help with cells.find and do if looping

    Hello wellsw,

    Try this...
        Dim C As Range
        Dim FirstAddress As String
        Dim LR As Long
        Dim Rng As Range
        
            Set Rng = Range("C2:C15")
            
                Set C = Rng.Find("#N/A", , xlValues, xlWhole, xlByRows, xlNext, False, False, False)
                
                If Not C Is Nothing Then
                    FirstAddress = C.Address
                    LR = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row
                    
                    Do
                        LR = LR + 1
                        Cells(C.Row, 1).Resize(1, 4).Copy Sheet2.Cells(LR, "A")
                        Set C = Rng.FindNext(C)
                        If C Is Nothing Then Exit Do
                    Loop Until C.Address = FirstAddress
                End If
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,788

    Re: Need help with cells.find and do if looping

    The reason that the Find is looking in all cells, and not just column C, is that is what is specified. The code specifies
    Cells.Find
    and Cells is a reference to the entire worksheet. You have used

    With Range("C2:C15")
    but then the code never actually takes advantage of that With. To do that you want something like this:

    With Range("C2:C15")
    
    Set C = .Find("#N/A")
    The .Find means "Do a find on the range I have specified in my With statement."

    However, that doesn't solve your problem I just am trying to do a little education there. The bigger problem is that a Find can only find a string, it cannot find cells that fail to match a string. That would have to be done as an explicit loop. I have suggested one solution here. You will also note that I declare all variables, which I highly recommend (along with Option Explicit, which is automatically provided in new modules if you set Tools, Options, Require Variable Declaration).

    I have tested this code on your file and I believe it does what you want:

    Option Explicit
    
    Sub Test()
    
       Dim C As Range
       Dim LR As Long
          
       For Each C In Range("C2:C15")
          If C.Text <> "#N/A" Then
             LR = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row
             Union(Cells(C.Row, 1), Cells(C.Row, 2), Cells(C.Row, 3), Cells(C.Row, 4)).Copy Sheet2.Cells(LR + 1, "A")
          End If
       Next C
    
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Registered User
    Join Date
    12-05-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    35

    Re: Need help with cells.find and do if looping

    6StringJazzer
    That is exactly what I needed... and thanks so much for breaking it down for me... that makes it easier to understand so I can adopt it for other applications!!

+ 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. [SOLVED] Looping Find Function
    By hkayani08 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-02-2012, 10:05 AM
  2. looping in the find command
    By jmgriffin101 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2009, 06:50 AM
  3. Looping Find
    By Gus80 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-05-2008, 06:34 PM
  4. Looping the Find Function
    By maverick_abhi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-04-2007, 01:54 AM
  5. Looping Macro - using find
    By DBaggs in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-17-2005, 01:34 PM

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