Problem with looping using FindNext

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
14,072
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have the following macro that works as expected. It finds only the value of the variable 'response' in column S and copies the appropriate range from the found rows to column C of Sheet2 starting in row 9. Cell C8 of Sheet2 has a header in it.
Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim response As String, sAddr As String
    Dim foundResponse As Range
    response = InputBox("Please enter the string to find.")
    If response = "" Then
        MsgBox ("You have not entered a string.")
        Exit Sub
    End If
    Set foundResponse = Sheets("Sheet1").Range("S:S").Find(response, LookIn:=xlValues, lookat:=xlPart)
    If Not foundResponse Is Nothing Then
        sAddr = foundResponse.Address
        Do
            Sheets("Sheet1").Range("F" & foundResponse.Row & ":M" & foundResponse.Row).Copy _
                Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, "C").End(xlUp).Offset(1, 0)
            Set foundResponse = Sheets("Sheet1").Range("S:S").FindNext(foundResponse)
        Loop While foundResponse.Address <> sAddr
        sAddr = ""
    Else
        MsgBox ("String not found.")
    End If
    Application.ScreenUpdating = True
End Sub
The following macro (which is what I tried initially) copies the range from every row with data in column S. The 'FindNext' doesn't limit the "find" to only the value of the variable "response".
Code:
Sub CopyData2()
    Application.ScreenUpdating = False
    Dim response As String, sAddr As String
    Dim lastRow As Long
    Dim foundResponse As Range
    response = InputBox("Please enter the string to find.")
    If response = "" Then
        MsgBox ("You have not entered a string.")
        Exit Sub
    End If
    Set foundResponse = Sheets("Sheet1").Range("S:S").Find(response, LookIn:=xlValues, lookat:=xlPart)
    If Not foundResponse Is Nothing Then
        sAddr = foundResponse.Address
        Do
            lastRow = Sheets("Sheet2").Columns(3).Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
            Sheets("Sheet1").Range("F" & foundResponse.Row & ":M" & foundResponse.Row).Copy Sheets("Sheet2").Cells(lastRow, 3)
            Set foundResponse = Sheets("Sheet1").Range("S:S").FindNext(foundResponse)
        Loop While foundResponse.Address <> sAddr
        sAddr = ""
    Else
        MsgBox ("String not found.")
    End If
    Application.ScreenUpdating = True
End Sub
I can't seem to figure out why the second macro isn't working as expected. Any suggestions would be greatly appreciated.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
It's because of your lastRow line, where you are using Find "*", that then means FindNext is looking for "*"
 
Upvote 0
Thank you so much. That makes perfect sense now that you pointed it out. :)
 
Last edited:
Upvote 0
Must admit, it took me a while to figure it out.
 
Upvote 0
I thought that
Code:
FindNext(foundResponse)
would find the next occurrence of foundResponse. I guess that's not the case.
 
Upvote 0
No, it's FindNext(after), so its repeating the previous Find, but starting in cell foundResponse
 
Upvote 0
Thanks for that. By the way, it didn't take long for you to figure it out. ;)
 
Upvote 0

Forum statistics

Threads
1,223,787
Messages
6,174,563
Members
452,573
Latest member
Cpiet

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top