Hi everyone,
I was wondering whether anyone could please help me refine this code?
I'm probably on the wrong track, as I have no training, but I've managed to cobble together something that works for me - so far... And of course, predictably, I've hit a snag...
What i'm trying to do is to a sort of vlookup via an input box which launches from a userform. There is a worksheet entitled "JobsList" which has the "Matter Number" in Column A, and the "Client's Name" in Column B". I want the user to be able to click the "Find" button, enter a name, and have the matter number returned in a message box.
So far i've got this - which, as long as I enter the "Client's Name" in exactly the same format, returns the "Matter Number" - yay!
But, the user might not know the exact format to search (e.g. at present, if they search "Smith, John" it will return the matter number). This is not particularly useful. I hope to find a way so that if they typed "Smith" they will get all rows which contain "Smith" in column B. I realise that my code as set out above has no hope of being able to do this - but I have no idea how to get it to do so!
Similarly, there might be more than one "Smith" in column B. I hope to be able to find a way to return a MsgBox with all applicable results listed so that they can chose the correct one. Is it even possible to return a MsgBox with multiple results? If not, the loop should work, as they could just "Ok" through the "Smith" entries until they find the one they are looking for. The latter is not ideal, but if that's the way we have to do it, that's fine.
My googling has not produced any results - but as you can probably tell from the above, i'm pretty confused and have probably not been searching the right terms. Any tips or pointers would be gratefully appreciated.
Thank you in advance.
I was wondering whether anyone could please help me refine this code?
I'm probably on the wrong track, as I have no training, but I've managed to cobble together something that works for me - so far... And of course, predictably, I've hit a snag...
What i'm trying to do is to a sort of vlookup via an input box which launches from a userform. There is a worksheet entitled "JobsList" which has the "Matter Number" in Column A, and the "Client's Name" in Column B". I want the user to be able to click the "Find" button, enter a name, and have the matter number returned in a message box.
So far i've got this - which, as long as I enter the "Client's Name" in exactly the same format, returns the "Matter Number" - yay!
Code:
Private Sub Find_Click()
Dim xlSheet As Worksheet
Dim lastRow As Integer
Dim i As Long
Dim fName As String
Set xlSheet = ActiveWorkbook.Sheets("JobsList")
fName = Application.InputBox("Please enter a name to search", Title:="Find Matter Number")
Select Case WorksheetFunction.CountIf(xlSheet.Range("B:B"), fName)
Case 0
fName = 0
MsgBox ("There are no files with this name")
Case 1
lastRow = xlSheet.Range("B" & xlSheet.Rows.Count).End(-4162).Row
For i = 1 To lastRow
If InStr(1, xlSheet.Range("B" & i), fName) > 0 Then
MsgBox (xlSheet.Range("A" & i))
Exit For
End If
Next i
End Select
End Sub
Similarly, there might be more than one "Smith" in column B. I hope to be able to find a way to return a MsgBox with all applicable results listed so that they can chose the correct one. Is it even possible to return a MsgBox with multiple results? If not, the loop should work, as they could just "Ok" through the "Smith" entries until they find the one they are looking for. The latter is not ideal, but if that's the way we have to do it, that's fine.
My googling has not produced any results - but as you can probably tell from the above, i'm pretty confused and have probably not been searching the right terms. Any tips or pointers would be gratefully appreciated.
Thank you in advance.