Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Spreadsheets (https://www.askmehelpdesk.com/forumdisplay.php?f=395)
-   -   VBA Code Needed (https://www.askmehelpdesk.com/showthread.php?t=540451)

  • Jan 4, 2011, 08:48 AM
    dannac
    VBA Code Needed
    Workbook with sheets from 1999 to 2010.
    Columns filled to H... Rows varies, but approx 50 each sheet.
    Client "last name" in column E.

    Will insert a sheet called "Find" and insert a form button.
    Clicking button would open InputBox for name to search.

    VBA code would search sheets (Col E) and if name from input box is found
    The entire row from that particular sheet would be inserted on the "Find" sheet

    ... and there may be more than one instance of that particular name.
  • Jan 4, 2011, 09:02 AM
    ITstudent2006

    There is a bandaid approach to this if you weren't aware.

    1. Right-Click Sheet 1999
    2. Click Select All Sheets (this puts all sheets in group mode)
    3. CTRL+F
    4. Input Name

    *This will search all sheets for this name and show you the sheet it's on, if there are multiple instances it will start with the first sheet and go through to the last bringing up all occurrences of that name. Create a Macro if needed!


    Not sure if this is anyhelp as I am sure you know this already, but that's all I can offer.
  • Jan 5, 2011, 02:20 AM
    JBeaucaire

    A quick macro to collect the string rows with the string you enter onto the sheet called Find:

    Code:

    Option Explicit

    Sub FindCopy()
    Dim ws As Worksheet
    Dim MyStr As String
    Dim nmFind As Range
    Dim nmFirst As Range

    MyStr = Application.InputBox("Name to find and copy:", "Enter Name", Type:=2)
    If MyStr = "False" Or MyStr = "" Then Exit Sub
    On Error Resume Next

    For Each ws In Worksheets
        If ws.Name <> "Find" Then
            Set nmFind = ws.Range("E:E").Find(MyStr, LookIn:=xlValues, LookAt:=xlWhole)
            If Not nmFind Is Nothing Then
                Set nmFirst = nmFind
                Do
                    nmFind.EntireRow.Copy Range("A" & Rows.Count).End(xlUp).Offset(1)
                    Set nmFind = ws.Range("E:E").FindNext(nmFind)
                Loop Until nmFind.Address = nmFirst.Address
                Set nmFind = Nothing
                Set nmFirst = Nothing
            End If
        End If
    Next ws

    End Sub

  • Jan 5, 2011, 05:46 AM
    dannac

    ITstudent2006... I was not aware of that shortcut and it works well and will be very helpful.
    Thank You.

    JBeacaire... wow, this code works perfectly.
    What a time-saver this will be. Thanks so much.

    Will also help me learn more about VBA...
    As I decipher your code with my " VBA Idiot Book ".

    Very helpful... Thanks
  • Jan 6, 2011, 05:22 AM
    JBeaucaire

    Things to read up on to fully decode the macro:

    1. Dimensioning variables
    2. Application.InputBox method (as opposed to just InputBox method which has less controls)
    3. The .Find function
    4. The .FindNext function
    5. Use of a For/Next loop to run like commands repetitively, in this instance on each worksheet
    6. Use of a Do/Loop to run like commands repetitively until some criteria is achieved to break the loop, in this instance if we find the string in a cell we've already found it in once already.
  • Jan 6, 2011, 10:39 AM
    dannac
    Thanks for the extra info.

    Visited your site... have a lunch on me.
  • Jan 8, 2011, 07:05 AM
    JBeaucaire

    Very kind of you! I will treat the wife this weekend.

    Hope you find lots of useful tips/code on the site, too, that's what it's really for. Let me know if you think of something you think I should include as a general macro/function.

    Thanks again.
  • Jan 27, 2011, 12:01 PM
    dannac
    Code:

    Option Explicit

    Sub FindCopy()
    Dim ws As Worksheet
    Dim MyStr As String
    Dim nmFind As Range
    Dim nmFirst As Range

    MyStr = Application.InputBox("Name to find and copy:", "Enter Name", Type:=2)
    If MyStr = "False" Or MyStr = "" Then Exit Sub
    On Error Resume Next

    For Each ws In Worksheets
        If ws.Name <> "Find" Then
            Set nmFind = ws.Range("F:F").Find(MyStr, LookIn:=xlValues, LookAt:=xlWhole)
            If Not nmFind Is Nothing Then
                Set nmFirst = nmFind
                Do
                    nmFind.EntireRow.Copy Range("A" & Rows.Count).End(xlUp).Offset(1)
                    Set nmFind = ws.Range("F:F").FindNext(nmFind)
                Loop Until nmFind.Address = nmFirst.Address
                Set nmFind = Nothing
                Set nmFirst = Nothing
            End If
        End If
    Next ws

    End Sub

    The code above works fine as for pulling in the row that contains the name from the input box.

    But I've noticed it brings in the first instance only... would need it to bring in all instances of that name.

    Some sheets may have no names... some one name... some four names, etc...

    Is this a hard fix ?
  • Jan 28, 2011, 11:32 AM
    JBeaucaire

    The code includes DO loop that copies each row found to the next empty row on the FIND sheet. I've used it, so it's the correct syntax.

    If you're not getting that behavior with your workbook, I'd need to see it to spot was was causing other rows to be ignored.

    One suspect is hidden/unseen spaces in the cells that we're matching to. These strings are not the same:
    Code:

    "John Doe"
    "John  Doe"
    "John Doe "

  • Jan 28, 2011, 01:32 PM
    dannac

    Thanks for the explanations to both my post.

    Looking back at the code for this one...

    I had changed the lookup column in the top part of the code, and did not make the same adjustment in the bottom part of the code.

    Works like it should... thank you.

  • All times are GMT -7. The time now is 03:32 AM.