Consulting

Results 1 to 13 of 13

Thread: Search Worksheet for Name if found copy to new workbook

  1. #1

    Search Worksheet for Name if found copy to new workbook

    I have 5 separate workbooks that contain lists of employee first names, and employee last names(as well as phone numbers emails etc). I want to open my workbook that contains an input box, enter an employee first name and have vba search the 5 workbooks, and if found (even a partial match, like Mitch and Mitchell) copy the entire row to the workbook that I have my input box.

    Is this an attainable function for excel vba to do?

  2. #2
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    It's possible but you have to attach some sample file

  3. #3
    Sure thing...attached are a few garbage data workbooks(of course the actual workbooks will contain many many more rows, but this should give ya a good feel of what I want to accomplish)
    Attached Files Attached Files

  4. #4
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    [vba]Public Sub InputBox()
    Dim EName As String
    Set sh = ThisWorkbook.Sheets(1)
    LR = sh.Cells(Rows.Count, "A").End(xlUp).Row
    EName = Application.InputBox(Prompt:="Please Enter the Employee's First Name to Search For.", Title:="Employee Search")
    if EName = "" then exit sub
    Dim strFile As String
    mFolder = "C:\test\" ' <<<<<<<<< only search workbooks here
    strFile = Dir(mFolder & "*.xls*")
    Application.ScreenUpdating = False
    Do While strFile <> ""
    Workbooks.Open mFolder & strFile
    Set Rng = ActiveSheet.UsedRange
    Set c = Rng.Find(EName, LookIn:=xlValues)
    If Not c Is Nothing Then
    Rows(c.Row).Copy sh.Cells(LR, 1)
    ActiveWorkbook.Close True
    Exit Do
    End If
    ActiveWorkbook.Close True
    strFile = Dir
    Loop
    Application.ScreenUpdating = True
    End Sub[/vba]

  5. #5
    I just thought about one more feature (if possible) to have added. Once the results from the 1st search are returned to the worksheet, would I be able to clear the worksheet of the 1st search results then (once added) push a button on the worksheet to launch the search form to perform a subsequent search?


    Thank you for that code btw, I will test and provide feedback

  6. #6
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    To clear the sheet whenever you run this SUB, after the line "Set sh = ...." add
    [vba]sh.Cells.ClearContents[/vba]
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  7. #7
    Quote Originally Posted by patel
    [vba]Public Sub InputBox()
    Dim EName As String
    Set sh = ThisWorkbook.Sheets(1)
    LR = sh.Cells(Rows.Count, "A").End(xlUp).Row
    EName = Application.InputBox(Prompt:="Please Enter the Employee's First Name to Search For.", Title:="Employee Search")
    if EName = "" then exit sub
    Dim strFile As String
    mFolder = "C:\test\" ' <<<<<<<<< only search workbooks here
    strFile = Dir(mFolder & "*.xls*")
    Application.ScreenUpdating = False
    Do While strFile <> ""
    Workbooks.Open mFolder & strFile
    Set Rng = ActiveSheet.UsedRange
    Set c = Rng.Find(EName, LookIn:=xlValues)
    If Not c Is Nothing Then
    Rows(c.Row).Copy sh.Cells(LR, 1)
    ActiveWorkbook.Close True
    Exit Do
    End If
    ActiveWorkbook.Close True
    strFile = Dir
    Loop
    Application.ScreenUpdating = True
    End Sub[/vba]
    The code is not opening any of the other workbooks. mFolder is returning a value but strFile is not. If I use debug.print and step thro the code strFile remains null.

  8. #8
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    in wich folder you have the files ? did you see this line ?
    mFolder = "C:\test\" ' <<<<<<<<< only search workbooks here

  9. #9
    Yes, I modified that path to
    [vba]
    mFolder = "C:\Users\Administrator\Documents\Testing\Excel\Templates\"
    [/vba]

  10. #10
    BUMP ---- Any words of wisdom on what could rectify this?

  11. #11
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    The code works on my testfile, attach again your file with new code

  12. #12
    Quote Originally Posted by patel
    The code works on my testfile, attach again your file with new code
    Attached is the workbook with code I use
    Attached Files Attached Files

  13. #13
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    maybe your file is damaged, try this attached
    Attached Files Attached Files

Posting Permissions

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