FindNext doesnt work?

Whistler

Board Regular
Joined
Jul 14, 2011
Messages
61
Hi, i need some help with a code below, in database there are 3 entreys which should be found bat after runnnig the code is returning only first one.

Code:
Sub Go_Click()
Dim ws As Worksheet
Dim ws2 As Worksheet
Set ws = Worksheets("DataBase")
Set ws2 = Worksheets("Review")
Dim What As String
Dim Where As Range
Set Where = ws.Range("B:B")
Dim Found As Range
Dim iRow As Long
Dim firstAddress As String
'find first empty row
iRow = ws2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
What = ws2.Range("C4")
With Where
Set Found = .Find(What:=What, After:=Range("B2"), LookAt:=xlPart, MatchCase:=False)
 If Not Found Is Nothing Then
    firstAddress = Found.Address
        Do
        Found.Copy
        ws2.Cells(iRow, 2).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Set Found = .FindNext(Found)
        Loop While Not Found Is Nothing And Found.Address <> firstAddress
 End If
End With
End Sub

Thanks for your help
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
How do you know? Your code will overwrite the same destination cell each time.
 
Upvote 0
You might try this.
Code:
Do
    ws2.Cells(iRow, 2).Value = Found.Value
    Set Found = .FindNext(Found)
Loop While Not Found Is Nothing And Found.Address <> firstAddress

You might want to consider that iRow doesn't change in that loop.
As written and altered, the loop repeatedly writes values to the same cell.

I just realized, it might write the same what value to that cell every time.
 
Last edited:
Upvote 0
Code:
Sub Go_Click()
Dim ws As Worksheet
Dim ws2 As Worksheet
Set ws = Worksheets("DataBase")
Set ws2 = Worksheets("Review")
Dim What As String
Dim Where As Range
Set Where = ws.Range("B:B")
Dim Found As Range
Dim iRow As Long
Dim firstAddress As String
What = ws2.Range("C4")
With Where
Set Found = .Find(What:=What, After:=Range("B2"), LookAt:=xlPart, MatchCase:=False)
 If Not Found Is Nothing Then
    firstAddress = Found.Address
        Do
        iRow = ws2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
        Found.Copy
        ws2.Cells(iRow, 2).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Set Found = .FindNext(Found)
        Loop While Not Found Is Nothing And Found.Address <> firstAddress
 End If
End With
End Sub

Sorted

Thanks mikerickson:)
 
Upvote 0
Code:
Do
    ws2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = Found.Value
    Set Found = .FindNext(Found)
Loop While Not Found Is Nothing And Found.Address <> firstAddress
 
Upvote 0
Thanks very much for help I didnt realie that I'm overwriting that cell.

I have another porblem, at the moment my cde is copying only one cell, what if I would like to copy few cells from the founded row?

Could anyone put me on the right direction?

Thanks
 
Upvote 0
The .Resize property might be useful, also .Offset, it depends on what you mean by "few cells from the found cell."
 
Upvote 0
If AA20 been found then I would like to copy AA20:AK20

I'am trying to use resize method but I have problem with selecting that found cell, code below:

Code:
Set ws = Worksheets("DataBase")
Set ws2 = Worksheets("Review")
Dim What As String
Dim Where As Range
Set Where = ws.Range("B:B")
Dim Found, Found2 As Range
Dim iRow As Long
Dim firstAddress As String
Dim Respond
Dim numRows, numColums
What = ws2.Range("C4")
With Where
Set Found = .Find(What:=What, After:=Range("B2"), LookAt:=xlPart, MatchCase:=False)
 If Not Found Is Nothing Then
    firstAddress = Found.Address
        Do
        iRow = ws2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
        numRows = Found.Rows.Count
        numColumns = Found.Columns.Count
        Found.Select
        Found2 = Selection.Resize(numRows + 11, numColumns)
        Found2.Copy
        ws2.Cells(iRow, 2).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Set Found = .FindNext(Found)
        Loop While Not Found Is Nothing And Found.Address <> firstAddress
 Else
 
 Respond = MsgBox("No records found", vbOKOnly)
 End If
  
End With
End Sub

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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