Consulting

Results 1 to 19 of 19

Thread: VBA search

  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location

    VBA search

    Hi Guys, I have a Excel table consist of Customer Name and Account.Example: John Doe (in A1) and 123 (in A2). Then based on bank reference ( 0000100 FT transfer 00100 John 000100 ) i woud like to have a VBA code that will seach John from then bank reference and match it to the table, Once matched, return the Customer full name and account number. hope i make myself clear. and look forward to any discussion and solution. cheers

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum.

    upload a sample file to work with.
    provide the desired output in a blank sheet.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location
    Quote Originally Posted by mancubus View Post
    welcome to the forum.

    upload a sample file to work with.
    provide the desired output in a blank sheet.
    Attached Files Attached Files

  4. #4
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location
    hi mancubus, thank you for your reply, have attached as book 1.

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Regarding the input-output, you only need VLOOKUP.


    you have a named range called CUST

    lookup values are in Column A (starting at A5)
    lookup table is CUST
    Customer details are in the 3rd column of CUST
    Accounts are in the 4th column of CUST

    Formula in B5
    PHP Code:
    =VLOOKUP(A5,CUST,3,0
    Formula in C5
    PHP Code:
    =VLOOKUP(A5,CUST,4,0
    copied down to desired row.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location
    Quote Originally Posted by mancubus View Post
    Regarding the input-output, you only need VLOOKUP.


    you have a named range called CUST

    lookup values are in Column A (starting at A5)
    lookup table is CUST
    Customer details are in the 3rd column of CUST
    Accounts are in the 4th column of CUST

    Formula in B5
    PHP Code:
    =VLOOKUP(A5,CUST,3,0
    Formula in C5
    PHP Code:
    =VLOOKUP(A5,CUST,4,0
    copied down to desired row.






    Hi mancubus,

    thank you for your responses, and sorry i have make an mistake, there should not be any bank ref column in the name range (cust) thus vlookup is not usable.

    i have attached a corrected excel for your easy reference.
    Attached Files Attached Files

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    please dont quote previous messages unless you need highlighting a specific bit. in that case just qoute that small bit not the whole message.


    that said, you are trying to find partial string from column A in column F.


    since there is no common pattern for partian strings, according tome, the best way is to use a helper column and enter corresponding values from column F manually. this enables using VLOOKUP function.

    i did it for you in the attached file.

    Column D in "task" sheet is the helper column.

    if you can set up a pattern then we can provide a either formula or vba solution without using the helper column.

    examine the range A21:C29 and its header on "task" sheet.

    good luck.
    Attached Files Attached Files
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location
    Hi Mancubus,

    Once again, thank you so much for guiding me thru.

    Yes the LOOKUP NAMES in the Detail Description is not consistent at their position.

    So i am thinking (but not sure if is feasible) is that i will set up a look up table. so everything there is the Detail Description, Example: < BENDIGO BANK 0000000000000000 DELANY ADVERT BAL > then the code will search the look up table one by one till they find < DELANY > so is probably using a loop with 2 exit condition ( when a match is found then return something and when the search finish the whole look up table and return nothing)

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    yes. i oftenly do that.
    when the formula returns N/A error in a cell, i understand there new items to add, and update the look up table accordingly.

    in that case vlookup will produce the desired results.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  10. #10
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location
    Mancubus,

    can u look at this excel and see if my logic is correct and whether is feasible?

    thanks alot^^
    Attached Files Attached Files

  11. #11
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    keep the table and the named range (CUST) in your file. because the following code uses them.
    update this table so that it will cover all the (partial) values in column A.

    Sub vbax_59174_find_search_list_in_column_return_corresponding_info()
    
        Dim srcList
        Dim foundCell As Range
        Dim i As Long
        
        srcList = Range("CUST").Value
        
        With Worksheets("task")
            For i = LBound(srcList) To UBound(srcList)
                Set foundCell = .Columns(1).Find(What:=srcList(i, 1), _
                    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
                If Not foundCell Is Nothing Then
                    foundCell.Offset(, 1).Value = srcList(i, 2)
                    foundCell.Offset(, 2).Value = srcList(i, 3)
                End If
            Next i
    End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  12. #12
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location
    Mancubus,

    Thank you so much, took me so long to understand your code. its really amazing. will have to study more on VBA.

    much appreciated.

    Cheers
    Ken

  13. #13
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    pls mark the thread as solved from Thread Tools dropdown for future references to the thread.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  14. #14
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location
    Mancubus,

    sorry, i have some trouble with the code,

    lets say i expand and have DYMOCKS BRISBANE0000000000000000 DY0024 at cell A10, the code will not reflect the result at B10 and C10.

    not sure if i fully understand the code. hope u can explain a little to me.

    Many Thanks

    Cheers
    Ken

  15. #15
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    so the values in column A are not unique?
    there are multiple occurences of a "Detail Description"?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  16. #16
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location
    Yes it will expand. and have duplicates... also in the future, i will expand the look up table as well.. so will have more information.

  17. #17
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    this may be called classic to do a 'multiple instance search' as you can find it anywhere..

     Sub vbax_59174_find_search_list_in_column_return_corresponding_info_multi_instance()
          
        Dim srcList
        Dim FoundCell As Range
        Dim i As Long
        Dim FirstAddress As String
        srcList = Range("CUST").Value
         
        With Worksheets("task")
            For i = LBound(srcList) To UBound(srcList)
                Set FoundCell = .UsedRange.Columns(1).Find(What:=srcList(i, 1), _
                    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
        
                If Not FoundCell Is Nothing Then
                    FirstAddress = FoundCell.Address
                End If
            
                Do Until FoundCell Is Nothing
                    FoundCell.Offset(, 1).Value = srcList(i, 2)
                    FoundCell.Offset(, 2).Value = srcList(i, 3)
                    Set FoundCell = .UsedRange.Columns(1).FindNext(After:=FoundCell)
                    If FoundCell.Address = FirstAddress Then Exit Do
                Loop
            Next i
        End With
         
     End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  18. #18
    VBAX Regular
    Joined
    Apr 2017
    Posts
    10
    Location
    mancubus,

    this is so cool!!! works perfectly! thank you so much^^

    appreciate your kind assistance.

    Cheers
    Ken

  19. #19
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome.
    glad it helped.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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