Please Help!!!While Loop and FindNext

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Guest
I am trying to search the values in two columns. Since some of the values are repeated, I have created while loop to match their row. However, I got traped in an endless loop
Could someone please help me
the following is the loop

Do While fRow < gRow
Set f = Columns(3).Cells.FindNext(f)
Loop

Thanks in advance
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You didn't mention in your posting what fRow and gRow are. However, since you do not change either one of them in your loop, if fRow<gRow at the beginning, then fRow<gRow on every iteration so the loop will never exit, regardless of the success or failure of the search operation.

If you want to provide a bit more information or clarification regarding the problem you are trying to solve, I'm sure you will get lots of help with a loop that will do exactly what you want.
 
Upvote 0
I am trying to create a search userform to search more than two columns of data(but two columns for now) and the data has repeated value. So, the users can enter 2 values to select a specific value or 1 value to select the first value found.
So what I did was to get the row number of two values entered by user. If those two values are on the same row select the value, if not, it loops and try to find the values on the same row
So, fRow is the 3rd row and gRow is the 4th row.
When I run the macro, I got all kinds of error messages in the last outer Else block and especially on the following two lines

Set f = Columns(3).Cells.FindNext(f)
fRow = f.Row

I have been spending too much time debugging the code and my boss is getting angry about my slowlyness. So, I haven't got much time left to finish this project
The following is the main part of the entire code, I would be greatly appreciated if you could spend some time to look over and correct any logic mistakes or syntex in there.
Thanks in advance
If you need more information please let me know
The following is the code:

Private Sub SearchButton_Click()
MillToFind = tbMillToFind.Text
GrdeToFind = tbGrdeToFind.Text
ColrToFind = tbColrToFind.Text
BswtToFind = tbBswtToFind.Text
LongGradeDescriptionToFind = tbLongGradeDescription.Text

Set e = Columns(2).Find(What:=GrdeToFind, LookAt:=xlWhole)
Set f = Columns(3).Find(What:=ColrToFind, LookAt:=xlWhole)
Set g = Columns(4).Find(What:=BswtToFind, LookAt:=xlWhole)


If f Is Nothing Then
MsgBox ColrToFind & "Colour Code was not found.", vbInformation, "Result"

With tbColrToFind
.SelStart = 0
.SelLength = 100
.SetFocus
End With
Exit Sub
ElseIf f = "" Then
If g Is Nothing Then
MsgBox BswtToFind & "Basis Weight was not found.", vbInformation, "Result"
With tbBswtToFind
.SelStart = 0
.SelLength = 100
.SetFocus
End With
Exit Sub
ElseIf g = "" Then

Else
g.Activate
Unload Me
End If



Else
If g Is Nothing Then

MsgBox BswtToFind & "Basis Weight was not found.", vbInformation, "Result"

With tbBswtToFind
.SelStart = 0
.SelLength = 100
.SetFocus
End With
Exit Sub
ElseIf g = "" Then
f.Activate
Unload Me
ElseIf f.Address = g.Offset(0, -1).Address Then
f.Activate:
Unload Me
Else



Dim fRow As Double
Dim gRow As Double

fRow = f.Row
gRow = g.Row
'if f's row number = g's row number
If fRow = gRow Then
'show data
fActivate:
Unload Me
'elseif f's row number < g's row number
ElseIf fRow < gRow Then
MsgBox "fRow < gRow"
'while f's row number < g's row number

Do While fRow < gRow

Set f = Columns(3).Cells.FindNext(f)
fRow = f.Row

'endloop

Loop
MsgBox fRow & " " & gRow
'if f's row number = g's row number
If fRow = gRow Then
'message found
f.Activate:
Unload Me
Else
' 'message notfound
MsgBox "Message not Found"
End If
Else
MsgBox "frow > grow"
'while f's row number > g's row number
Do While fRow > gRow
'g = g.findnext

Set g = Columns(4).Cells.FindNext(g)
gRow = g.Row
'endloop
Loop
'if f's row number = g's row number
If fRow = gRow Then
'msgbox found
g.Activate:
Unload Me

Else

'msgbox not found
MsgBox "Data not found"
End If


End If





End If

End If





End Sub
 
Upvote 0

Forum statistics

Threads
1,223,374
Messages
6,171,722
Members
452,418
Latest member
kennettz

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