+ Reply to Thread
Results 1 to 4 of 4

Search and return value to the left? Variables!

Hybrid View

  1. #1
    Registered User
    Join Date
    04-17-2011
    Location
    Orlando
    MS-Off Ver
    Excel 2007
    Posts
    27

    Exclamation Search and return value to the left? Variables!

    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"

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Search and return value to the left? Variables!

    Do you mean like this?

    Sub 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
    abousetta

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search and return value to the left? Variables!

    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:
    Set FoundRng = Ws1.Range(Ws1.Cells(2, 13), Ws1.Cells(Rows.Count, 13).End(xlUp)).Cells.Find(SrchString)
    Should really be this:
    Set FoundRng = Ws1.Range(Ws1.Cells(2, 13), Ws1.Cells(ws1.Rows.Count, 13).End(xlUp)).Cells.Find(SrchString)
    Or this:
    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 the icon 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!)

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Search and return value to the left? Variables!

    Excellent point JB. Catching these early on saves a lot of time down the road.

    abousetta

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1