+ Reply to Thread
Results 1 to 6 of 6

Searching for multiple names and variants

Hybrid View

CFlack8472 Searching for multiple names... 03-19-2012, 11:13 AM
Dennis7849 Re: Searching for multiple... 03-19-2012, 11:58 AM
Leith Ross Re: Searching for multiple... 03-19-2012, 12:19 PM
Leith Ross Re: Searching for multiple... 03-19-2012, 06:35 PM
CFlack8472 Re: Searching for multiple... 03-19-2012, 08:27 PM
Leith Ross Re: Searching for multiple... 03-19-2012, 08:30 PM
  1. #1
    Registered User
    Join Date
    10-14-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    25

    Searching for multiple names and variants

    I have a worksheet which I have created from our team’s accounting software. We have over 300 employees and you can see expense entries for their names in their various formats for example:

    MANAGED SOLUTIONS - 26-06-11 Justine Mitchell MANAGED SOLUTIONS - 17-07-11 Maruf Ahmad BURR, JULIAN DAVID - OH Admin TRAVEL SOLUTIONS - 6/7/2011 BARNHAM/TONYMR 680 4 etc.

    I need to design a macro or something where I can enter all the possible variants of people’s names as they occur in the worksheet (as you can see from the emboldened entries above and on the attached .xls) I need it to take across the relevant cost code, date and amount columns and ideally place them into a new worksheet along with the correct name in the format First Name, Surname.

    So for the above example Justine Mitchell and Maruf Ahmad are correct however Burr, Julian David should become Julian Burr BARNHAM/TONYMR should be Tony Barnham etc and see the expenses, cost code date and amount attributable to each member of the team. As you can see in its raw format it is pretty useless when I try and pivot this data.

    I am hoping that it is possible to enter all the variants of the people’s names and have a nice set of data at the end of the process and this will be easy and presentable for a pivot table.

    I am unsure of how to do the above but if someone could give me some VBA code and do the hard part so that I can manually input each of the variants of the names that the data in putters are using I would be extremely grateful.

    Please let me know if you need more info. I am new to VBA

    Regards
    Chris.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    03-14-2012
    Location
    Arizona USA
    MS-Off Ver
    Excel 2000/2007
    Posts
    408

    Re: Searching for multiple names and variants

    What would help with this is a before and after sample. You included the input(aka the before sample).

    Also if you have a complete list of all the names that you need to scan for, that would help.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Searching for multiple names and variants

    Hello Chris,

    Do you want to replace the variant name in column "C" (Description and Memo) with the proper forename and surname? Forename in this case is one or more given names like "Julian David".
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Searching for multiple names and variants

    Hello Chris,

    The macro below has been added to the attached workbook. There are 2 additional sheets along with you original data. The sheet "Expense Summary" is where the macro outputs the person's forename and surname along with the cost code, date and amount. The third sheet "Names List" contains the proper name, forename and surname, along with the possible variations in the columns to the right.

    Currently, there are five additional variations allowed. If you need more, add a label in row 1 for the new column. The macro uses the column headers to automatically size the dictionary.
    
    Option Explicit
    
    ' Thread:  http://www.excelforum.com/excel-programming/820336-searching-for-multiple-names-and-variants.html
    ' Poster:  CFlack8472
    ' Written: March 19, 2012
    ' Author:  Leith Ross
    
    Sub ExpenseSummary()
    
        Dim Cell As Range
        Dim Dict As Object
        Dim DstWks As Worksheet
        Dim Found As Boolean
        Dim Item As Variant
        Dim Key As Variant
        Dim N As Long
        Dim NameData As Variant
        Dim R As Long
        Dim Rng As Range
        Dim RngEnd As Range
        Dim SrcWks As Worksheet
        
            Set SrcWks = Sheet1
            
            Set DstWks = Sheet2
            
            NameData = Sheet3.UsedRange.Offset(1, 0).Resize(Sheet3.UsedRange.Rows.Count - 1).Value
            
            Set Dict = CreateObject("Scripting.Dictionary")
            Dict.CompareMode = vbTextCompare
            
                For Each Item In NameData
                    If R = UBound(NameData, 1) Then R = 1 Else R = R + 1
                    If Item <> "" Then
                        Key = Trim(Item)
                        If Not Dict.Exists(Key) Then
                            Dict.Add Key, R + 1
                        End If
                    End If
                Next Item
                
                Set Rng = SrcWks.Range("C2")
                Set RngEnd = SrcWks.Cells(Rows.Count, "C").End(xlUp)
                Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, SrcWks.Range(Rng, RngEnd))
                
                Application.ScreenUpdating = False
                
                DstWks.UsedRange.Offset(1, 0).ClearContents
                N = 2
                
                For Each Cell In Rng
                    For Each Key In Dict.Keys
                        R = Dict(Key)
                        If InStr(1, Cell.Value, Key, vbTextCompare) Then
                            Found = True
                            DstWks.Cells(N, "A") = Sheet3.Cells(R, "A").Value
                            DstWks.Cells(N, "B") = Sheet3.Cells(R, "B").Value
                            DstWks.Cells(N, "C").Resize(1, 2) = SrcWks.Cells(Cell.Row, "A").Resize(1, 2).Value
                            DstWks.Cells(N, "E") = SrcWks.Cells(Cell.Row, "D").Value
                        End If
                    Next Key
                    If Found Then N = N + 1: Found = False
                Next Cell
                
                Application.ScreenUpdating = True
                
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-14-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Searching for multiple names and variants

    Quote Originally Posted by Leith Ross View Post
    Hello Chris,

    The macro below has been added to the attached workbook. There are 2 additional sheets along with you original data. The sheet "Expense Summary" is where the macro outputs the person's forename and surname along with the cost code, date and amount. The third sheet "Names List" contains the proper name, forename and surname, along with the possible variations in the columns to the right.
    You're a hero, thank you very much! I will have a good look at it tomorrow when I'm at work. It's 00:26 here.

    I hope it didn't take you too long.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Searching for multiple names and variants

    Hello Chris,

    You're welcome. If you need anything changed, let me know.

+ 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