VBA FIND - use xlNext and XlPrevious at the same time

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
Hi!

I don't want to search for a value just after a cell, or before that. How could I perform that?

The value of A10 is in Cells of B1, B3, B7, and B16 too.

If I search for the value of A10, then i don't want to get B16, because it looks for the value with a direction of "xlNext". I need B1, than if I search again, then B3, and if I search again B7 and so on.

There is a button, and the following macro is added to it:

Sub searchActiveCellInColumnN()
Dim CurrValue As String
Dim foundcell As Range


CurrValue = ActiveCell.Value

If Len(CurrValue) = 0 Then Exit Sub

Set foundcell = Sheets(1).Columns("N:N").Find(What:=CurrValue, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False) 'SearchDirection:=xlNext, *



If Not foundcell Is Nothing Then
Sheets(1).Activate
foundcell.Select

Else
'not found
MsgBox CurrValue & Chr(10) & "Record Not Found", 48, "Not Found"

End If




End Sub



Any idea, how to make my button work, so it always looks for the active cell's content from the start of that column, and for every next "run" of the code it gives the next match in that column. Could you modify it? Have a very nice day! :)
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
How about
Code:
Sub searchActiveCellInColumnN()
Dim CurrValue As String
Static foundcell As Range

CurrValue = ActiveCell.Value

If Len(CurrValue) = 0 Then Exit Sub
If foundcell Is Nothing Then Set foundcell = Sheets(1).Range("N1048576")
Set foundcell = Sheets(1).Range("N:N").Find(CurrValue, foundcell, xlFormulas, xlPart, xlByRows, xlNext, False, , False)

If Not foundcell Is Nothing Then
   Sheets(1).Activate
   foundcell.Select
Else
   'not found
   MsgBox CurrValue & Chr(10) & "Record Not Found", 48, "Not Found"
End If
End Sub
 
Upvote 0
Fluff, one day I will help you everything back :D Thank you!! Perfect!

How about
Code:
Sub searchActiveCellInColumnN()
Dim CurrValue As String
Static foundcell As Range

CurrValue = ActiveCell.Value

If Len(CurrValue) = 0 Then Exit Sub
If foundcell Is Nothing Then Set foundcell = Sheets(1).Range("N1048576")
Set foundcell = Sheets(1).Range("N:N").Find(CurrValue, foundcell, xlFormulas, xlPart, xlByRows, xlNext, False, , False)

If Not foundcell Is Nothing Then
   Sheets(1).Activate
   foundcell.Select
Else
   'not found
   MsgBox CurrValue & Chr(10) & "Record Not Found", 48, "Not Found"
End If
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,625
Messages
6,120,598
Members
448,973
Latest member
ksonnia

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