+ Reply to Thread
Results 1 to 5 of 5

loop and findnext

  1. #1
    John
    Guest

    loop and findnext

    Can someone tell me why this won't find the next value in the loop... it
    stays on the first one and crashes my excel...

    heets("c").Select
    Set wks = ActiveSheet
    Set rngToSearch = wks.Columns(1)
    Set RngFound = rngToSearch.Find(what:="new", LookIn:=xlValues,
    lookat:=xlWhole)
    If RngFound Is Nothing Then
    MsgBox "Nothing"
    Else
    Do
    r = RngFound.Row
    Range("a" & r, "z" & r).Select
    With Selection.Interior
    .ColorIndex = 36
    .Pattern = xlSolid
    End With
    Range("a" & r).Select
    Set RngFound = rngToSearch.FindNext
    Loop Until RngFound Is Nothing
    End If
    End Sub

  2. #2
    Tom Ogilvy
    Guest

    Re: loop and findnext

    sheets("c").Select
    Set wks = ActiveSheet
    Set rngToSearch = wks.Columns(1)
    Set RngFound = rngToSearch.Find(what:="new", LookIn:=xlValues,
    lookat:=xlWhole)
    If RngFound Is Nothing Then
    MsgBox "Nothing"
    Else
    sAddr = RngFound.Address
    Do
    r = RngFound.Row
    Range("a" & r, "z" & r).Select
    With Selection.Interior
    .ColorIndex = 36
    .Pattern = xlSolid
    End With
    Range("a" & r).Select
    Set RngFound = rngToSearch.FindNext(rngFound)
    Loop Until RngFound Is Nothing or RngFound.Address = _
    sAddr
    End If
    End Sub

    --
    Regards,
    Tom Ogilvy

    "John" <John@discussions.microsoft.com> wrote in message
    news:E2A5624E-6E67-46B8-AACB-CDF640EA4F81@microsoft.com...
    > Can someone tell me why this won't find the next value in the loop... it
    > stays on the first one and crashes my excel...
    >
    > heets("c").Select
    > Set wks = ActiveSheet
    > Set rngToSearch = wks.Columns(1)
    > Set RngFound = rngToSearch.Find(what:="new", LookIn:=xlValues,
    > lookat:=xlWhole)
    > If RngFound Is Nothing Then
    > MsgBox "Nothing"
    > Else
    > Do
    > r = RngFound.Row
    > Range("a" & r, "z" & r).Select
    > With Selection.Interior
    > .ColorIndex = 36
    > .Pattern = xlSolid
    > End With
    > Range("a" & r).Select
    > Set RngFound = rngToSearch.FindNext
    > Loop Until RngFound Is Nothing
    > End If
    > End Sub




  3. #3
    John
    Guest

    Re: loop and findnext

    Thank you Tom, I appreciate the help.

    "Tom Ogilvy" wrote:

    > sheets("c").Select
    > Set wks = ActiveSheet
    > Set rngToSearch = wks.Columns(1)
    > Set RngFound = rngToSearch.Find(what:="new", LookIn:=xlValues,
    > lookat:=xlWhole)
    > If RngFound Is Nothing Then
    > MsgBox "Nothing"
    > Else
    > sAddr = RngFound.Address
    > Do
    > r = RngFound.Row
    > Range("a" & r, "z" & r).Select
    > With Selection.Interior
    > .ColorIndex = 36
    > .Pattern = xlSolid
    > End With
    > Range("a" & r).Select
    > Set RngFound = rngToSearch.FindNext(rngFound)
    > Loop Until RngFound Is Nothing or RngFound.Address = _
    > sAddr
    > End If
    > End Sub
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "John" <John@discussions.microsoft.com> wrote in message
    > news:E2A5624E-6E67-46B8-AACB-CDF640EA4F81@microsoft.com...
    > > Can someone tell me why this won't find the next value in the loop... it
    > > stays on the first one and crashes my excel...
    > >
    > > heets("c").Select
    > > Set wks = ActiveSheet
    > > Set rngToSearch = wks.Columns(1)
    > > Set RngFound = rngToSearch.Find(what:="new", LookIn:=xlValues,
    > > lookat:=xlWhole)
    > > If RngFound Is Nothing Then
    > > MsgBox "Nothing"
    > > Else
    > > Do
    > > r = RngFound.Row
    > > Range("a" & r, "z" & r).Select
    > > With Selection.Interior
    > > .ColorIndex = 36
    > > .Pattern = xlSolid
    > > End With
    > > Range("a" & r).Select
    > > Set RngFound = rngToSearch.FindNext
    > > Loop Until RngFound Is Nothing
    > > End If
    > > End Sub

    >
    >
    >


  4. #4
    FSt1
    Guest

    RE: loop and findnext

    hi,
    i copied your code and tried to run it. got caught in an endless loop. so i
    re-wrote i a little. your main problem was in the do loop.

    Sub macfindit()
    Dim wks As Worksheet
    Dim rngToSearch As Range
    Dim rngFound As Range
    Dim r As Long
    Dim addr As String

    'Sheets("Sheet1").Select
    Set wks = ActiveSheet
    Set rngToSearch = wks.Columns(1)
    Set rngFound = rngToSearch.Find(what:="new", _
    After:=Range("A1"), _
    LookIn:=xlValues, _
    SearchOrder:=xlColumns, _
    SearchDirection:=xlNext, _
    lookat:=xlWhole)

    If rngFound Is Nothing Then
    MsgBox "Nothing"
    Exit Sub
    Else
    addr = rngFound.Address
    Do
    r = rngFound.Row
    Range("a" & r, "z" & r).Select
    With Selection.Interior
    .ColorIndex = 36
    .Pattern = xlSolid
    End With
    Range("a" & r).Select
    Set rngFound = rngToSearch.FindNext(rngFound)
    Loop Until rngFound.Address = addr
    End If

    End Sub

    regards
    FSt1
    "John" wrote:

    > Can someone tell me why this won't find the next value in the loop... it
    > stays on the first one and crashes my excel...
    >
    > heets("c").Select
    > Set wks = ActiveSheet
    > Set rngToSearch = wks.Columns(1)
    > Set RngFound = rngToSearch.Find(what:="new", LookIn:=xlValues,
    > lookat:=xlWhole)
    > If RngFound Is Nothing Then
    > MsgBox "Nothing"
    > Else
    > Do
    > r = RngFound.Row
    > Range("a" & r, "z" & r).Select
    > With Selection.Interior
    > .ColorIndex = 36
    > .Pattern = xlSolid
    > End With
    > Range("a" & r).Select
    > Set RngFound = rngToSearch.FindNext
    > Loop Until RngFound Is Nothing
    > End If
    > End Sub


  5. #5
    John
    Guest

    RE: loop and findnext

    Thanks! That works as well.

    "FSt1" wrote:

    > hi,
    > i copied your code and tried to run it. got caught in an endless loop. so i
    > re-wrote i a little. your main problem was in the do loop.
    >
    > Sub macfindit()
    > Dim wks As Worksheet
    > Dim rngToSearch As Range
    > Dim rngFound As Range
    > Dim r As Long
    > Dim addr As String
    >
    > 'Sheets("Sheet1").Select
    > Set wks = ActiveSheet
    > Set rngToSearch = wks.Columns(1)
    > Set rngFound = rngToSearch.Find(what:="new", _
    > After:=Range("A1"), _
    > LookIn:=xlValues, _
    > SearchOrder:=xlColumns, _
    > SearchDirection:=xlNext, _
    > lookat:=xlWhole)
    >
    > If rngFound Is Nothing Then
    > MsgBox "Nothing"
    > Exit Sub
    > Else
    > addr = rngFound.Address
    > Do
    > r = rngFound.Row
    > Range("a" & r, "z" & r).Select
    > With Selection.Interior
    > .ColorIndex = 36
    > .Pattern = xlSolid
    > End With
    > Range("a" & r).Select
    > Set rngFound = rngToSearch.FindNext(rngFound)
    > Loop Until rngFound.Address = addr
    > End If
    >
    > End Sub
    >
    > regards
    > FSt1
    > "John" wrote:
    >
    > > Can someone tell me why this won't find the next value in the loop... it
    > > stays on the first one and crashes my excel...
    > >
    > > heets("c").Select
    > > Set wks = ActiveSheet
    > > Set rngToSearch = wks.Columns(1)
    > > Set RngFound = rngToSearch.Find(what:="new", LookIn:=xlValues,
    > > lookat:=xlWhole)
    > > If RngFound Is Nothing Then
    > > MsgBox "Nothing"
    > > Else
    > > Do
    > > r = RngFound.Row
    > > Range("a" & r, "z" & r).Select
    > > With Selection.Interior
    > > .ColorIndex = 36
    > > .Pattern = xlSolid
    > > End With
    > > Range("a" & r).Select
    > > Set RngFound = rngToSearch.FindNext
    > > Loop Until RngFound Is Nothing
    > > End If
    > > End Sub


+ 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