Hello,
I have one column with employee names (Column L) and and another column with employee ID numbers (Column M)
I need a macro that when the Employee ID number is searched it returns the Employees Name.
A previous thread helped me and gave me this:
Sub Crebsington() Dim SrchString As String Dim FoundRng As Range, oCell As Range Dim Ws1 As Worksheet Set Ws1 = Worksheets("Sheet1") SrchString = InputBox("Enter the employee number", "") If SrchString = "" Then Exit Sub Set FoundRng = Ws1.Range(Ws1.Cells(2, 13), Ws1.Cells(Rows.Count, 13).End(xlUp)).Cells.Find(SrchString) If Not FoundRng Is Nothing Then MsgBox "Employee No: " & SrchString & vbNewLine & "Employee Name: " & FoundRng.Offset(0, -1).Value End If End Sub
But I was hoping that I could Dim both the Employee Name and Employee number both as their own string somethting like this maybe:
For example: Dim EmployeeName as string Employee Name = FoundRng.Offset(0, -1).Value Msgbox(EmployeeName) Return "Employee1"
Do you mean like this?
abousettaSub TryMe() Dim SrchString As String Dim EmployeeName As String Dim FoundRng As Range Dim Ws1 As Worksheet Set Ws1 = Worksheets("Sheet1") SrchString = InputBox("Enter the employee number", "") If SrchString = "" Then Exit Sub Set FoundRng = Ws1.Range(Ws1.Cells(2, 13), Ws1.Cells(Rows.Count, 13).End(xlUp)).Cells.Find(SrchString) If Not FoundRng Is Nothing Then EmployeeName = FoundRng.Offset(0, -1).Value MsgBox (EmployeeName) End If End Sub
Just a note, when using a variable to direct commands applicable to a specific sheet object, attach that object to each range object in your commands, not just most of them, all of them.
So this line of code:
Should really be this:Set FoundRng = Ws1.Range(Ws1.Cells(2, 13), Ws1.Cells(Rows.Count, 13).End(xlUp)).Cells.Find(SrchString)
Or this:Set FoundRng = Ws1.Range(Ws1.Cells(2, 13), Ws1.Cells(ws1.Rows.Count, 13).End(xlUp)).Cells.Find(SrchString)
With ws1 Set FoundRng = .Range(.Cells(2, 13), .Cells(.Rows.Count, 13).End(xlUp)).Cells.Find(SrchString) End With
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Excellent point JB. Catching these early on saves a lot of time down the road.
abousetta
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks