+ Reply to Thread
Results 1 to 6 of 6

Search Text String separated by Commas and do a Vlookup based on results

Hybrid View

  1. #1
    Registered User
    Join Date
    06-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    51

    Search Text String separated by Commas and do a Vlookup based on results

    Hi All,
    Thank you in advance for your help.
    I have a string of invoice numbers, separated by commas. I am trying to figure out how to have a macro look in the string on column G, do a vlookup for each invoice # (separated by a comma), and then put the total in column H.

    I have attached an example spreadsheet. Column G:H are what I need to look for and paste results. Columns A:B are what the vlookup references.

    Thanks!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Search Text String separated by Commas and do a Vlookup based on results

    Function invoice_lookup(invoices As String)
    Dim i, Str
    Str = Split(invoices, ",")
    For i = LBound(Str) To UBound(Str)
        invoice_lookup = invoice_lookup + WorksheetFunction.VLookup(Str(i), Range("A4:B8"), 2, 0)
    Next
    End Function
    ?

  3. #3
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Search Text String separated by Commas and do a Vlookup based on results

    Quote Originally Posted by yudlugar View Post
    Function invoice_lookup(invoices As String)
    Dim i, Str
    Str = Split(invoices, ",")
    For i = LBound(Str) To UBound(Str)
        invoice_lookup = invoice_lookup + WorksheetFunction.VLookup(Str(i), Range("A4:B8"), 2, 0)
    Next
    End Function
    ?
    very good solution, but there are numbers in column A then
    invoice_lookup = invoice_lookup + WorksheetFunction.VLookup(Val(Str(i)), Range("A4:B8"), 2, 0)
    If solved remember to mark Thread as solved

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Search Text String separated by Commas and do a Vlookup based on results

    Try this:-
    Sub MG15Nov12
    Dim Rng         As Range
    Dim Dn          As Range
    Dim n           As Long
    Dim Dic         As Object
    Dim oSum        As Double
    Dim oRws        As Variant
    Set Rng = Range(Range("A4"), Range("A" & Rows.Count).End(xlUp))
     Set Dic = CreateObject("scripting.dictionary")
        Dic.CompareMode = vbTextCompare
        For Each Dn In Rng
            Set Dic(Dn.Value) = Dn
        Next
    
    Set Rng = Range(Range("G4"), Range("G" & Rows.Count).End(xlUp))
        For Each Dn In Rng
            oRws = Split(Dn, ",")
            For n = 0 To UBound(oRws)
                If Dic.exists(Val(oRws(n))) Then
                    oSum = oSum + Dic.Item(Val(oRws(n))).Offset(, 1)
                End If
            Next n
                Dn.Offset(, 1) = Format(oSum, "$0.00")
                oSum = 0
        Next Dn
    End Sub
    Regards Mick

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Search Text String separated by Commas and do a Vlookup based on results

    Sub LookupTotals()
    Application.ScreenUpdating = False
    LastRowInv = Range("G65536").End(xlUp).Row
    LastRowBal = Range("B65536").End(xlUp).Row
    Set Rng = Range("A4:B" & LastRowBal)
    For r = 4 To LastRowInv
      sum1 = 0
      arr = Split(Range("G" & r), ",")
      For i = 0 To UBound(arr)
        sum1 = sum1 + Application.WorksheetFunction.VLookup(Val(arr(i)), Rng, 2, False)
      Next
      Range("H" & r) = sum1
    Next r
    Application.ScreenUpdating = True
    End Sub

  6. #6
    Registered User
    Join Date
    06-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: Search Text String separated by Commas and do a Vlookup based on results

    Thank you all for your help! These worked perfectly. Have a great day.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Extracting Text Separated by Commas
    By davidmg_13 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-02-2013, 05:18 PM
  2. Creating a chart out of text separated by commas
    By MC UP in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 09-14-2012, 03:28 PM
  3. Search Data For Text String And Copy Results
    By Spencer in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-09-2011, 06:57 PM
  4. Replies: 7
    Last Post: 03-05-2010, 04:25 PM
  5. Colum of emails to row of text separated by commas
    By debspecs in forum Excel General
    Replies: 1
    Last Post: 09-09-2008, 12:32 PM

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