davedave24
Programmer
Hello
I'm trying to find a string within a range, but only if the offset matches.
For example: I need to find the last occurence of, let's say, Cat 6.
My current code searches up from the bottom for the last instance of the string "Cat".
I need to adapt it so it checks if the Offset(0,1) = "6". If it does, we display some messagebox; if it doesn't, we continue with the Find upwards until we find Cat 6.
If I were searching for Cat 2, the code works, as it's the last entry in column A with "Cat"; I need the code to then continue upwards if the offset isn't what I'm looking for.
Here is my current code:
I have tried altering the line to say
but it produces the same problem that the code just stops on the last occurence and won't move upwards.
I'm trying to find a string within a range, but only if the offset matches.
For example: I need to find the last occurence of, let's say, Cat 6.
Code:
COLUMN A COLUMN B
Cat 1
Cat 6
Cat 1
Dog 4
Cat 6
Dog 3
Dog 8
Cat 2
My current code searches up from the bottom for the last instance of the string "Cat".
I need to adapt it so it checks if the Offset(0,1) = "6". If it does, we display some messagebox; if it doesn't, we continue with the Find upwards until we find Cat 6.
If I were searching for Cat 2, the code works, as it's the last entry in column A with "Cat"; I need the code to then continue upwards if the offset isn't what I'm looking for.
Here is my current code:
Code:
Dim sSerial As String
Dim sDocNo As String
Dim sCustomer As String
Dim sOnHireDate As String
Dim rngFind As Range
On Error Resume Next
sSerial = listOnHire.List(listOnHire.ListIndex, 1)
With Sheets("Hire Diary").Range("D5:D5000") 'this is the range we are searching
Set rngFind = .Find(What:=sSerial, _ 'this searches upwards from the last cell in the range
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If Not rngFind Is Nothing Then
Application.Goto rngFind, False
sDocNo = ActiveCell.Offset(0, -3)
sCustomer = ActiveCell.Offset(0, -2)
sOnHireDate = ActiveCell.Offset(0, 1)
MsgBox "Serial : " & vbTab & vbTab & sSerial & vbCrLf & _
"Item : " & vbTab & vbTab & listOnHire.List(listOnHire.ListIndex, 0) & vbCrLf & _
"Doc No : " & vbTab & vbTab & sDocNo & vbCrLf & _
"Customer : " & vbTab & sCustomer & vbCrLf & _
"On Hire Date : " & vbTab & Format(sOnHireDate, "DD-MM-YY") & vbCrLf & _
"Expiry Date : " & vbTab & listOnHire.List(listOnHire.ListIndex, 2), vbOKOnly + vbInformation, "Item currently on-hire"
Else
MsgBox "Nothing found"
End If
End With
I have tried altering the line to say
Code:
if Not rngFind is Nothing and rngfind.offset(0,1) = whatever