VBA - Excel Vlookup between 2 sheets

Hello

I have an Excel file that have 2 sheets

VENDOR - sheet
STORE sheet

In my vendor sheet, in Column A, i have all vendors and in Column C, i have all stores assigned to specific vendors.

Ex:
Vendor            STORE DELIVERY
KRAFT            1020, 4040, 0700
CAMPBELL      0700, 3030
TIDE                    4040, 0700, 0010
WONDER            3030, 2020
SNUGGLE            0010


In sheet STORE, i have the opposite. In column A, i have a list of all stores but the vendor is not populated.

Ex:
Store            Vendor
0010            
1020            
2020            
3030            
4040            
5050            
6060            
0700            


The macro i would like to do is to take all vendors from the VENDOR sheet and add them to the stores from the STORE sheet where the vendor deliver.

So the result would be at the end something like this:
Store            Vendor
0010            SNUGGLE, TIDE, SNUGGLE
1020            KRAFT
2020            WONDER
3030            CAMPBELL, WONDER
4040            KRAFT, TIDE
5050            
6060            
0700            KRAFT, CAMPBELL, TIDE


How can i do that?

Thanks again for your help
VENDOR---STORE.xlsm
LVL 11
Wilder1626Asked:
Who is Participating?
 
aikimarkConnect With a Mentor Commented:
This version iterates the store values in the Stores worksheet and populates the vendors cells.
Public Sub ParseStores2()
    Dim rngCell As Range
    Dim rngSrc As Range
    Dim rngTgt As Range
    Dim wksSrc As Worksheet
    Dim wksTgt As Worksheet
    Dim oRE As Object
    Dim oMatches As Object
    Dim oMatch As Object
    Dim dicUnique As Object
    Dim vItem As Variant
    
    Set dicUnique = CreateObject("scripting.dictionary")
    dicUnique.comparemode = VbCompareMethod.vbTextCompare
    
    Set oRE = CreateObject("vbscript.regexp")
    oRE.Global = True
    oRE.Pattern = "\b(\w+)\b"
    
    Set wksSrc = Worksheets("Vendor")
    
    For Each rngSrc In wksSrc.Range(wksSrc.Range("C2"), wksSrc.Range("C2").End(xlDown))
        Set oMatches = oRE.Execute(rngSrc.Value)
        'Debug.Print oMatches.Count, rngSrc.Value
        For Each oMatch In oMatches
            If dicUnique.exists(oMatch.Value) Then
                dicUnique(oMatch.Value) = dicUnique(oMatch.Value) & ", " & rngSrc.Offset(0, -2).Value
            Else
                dicUnique.Add oMatch.Value, rngSrc.Offset(0, -2).Value
            End If
        Next
    Next
    
    Set wksTgt = Worksheets("Store")
    Set rngTgt = wksTgt.Range("A2")
    'wksTgt.Range(rngTgt, rngTgt.Offset(dicUnique.Count - 1)).Value = Application.WorksheetFunction.Transpose(dicUnique.keys)
    For Each rngCell In wksTgt.Range(rngTgt, rngTgt.End(xlDown))
        If dicUnique.exists(rngCell.Value) Then
            rngCell.Offset(0, 2).Value = dicUnique(rngCell.Value)
        End If
    Next
    'Set rngTgt = rngTgt.Offset(0, 2)
    'wksTgt.Range(rngTgt, rngTgt.Offset(dicUnique.Count - 1)).Value = Application.WorksheetFunction.Transpose(dicUnique.items)

End Sub

Open in new window

0
 
gowflowCommented:
Are all the Stores In Col C in the sheet Vendor Comma separated ? like they are manually put there or produced by a system ?
gowflow
0
 
Wilder1626Author Commented:
90% of them yes, they are separated by a comma. I guess i may need to put comma everywhere. Do i?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Wilder1626Author Commented:
So 90% of them have comma, but 100% have a space between them.

Can it be a problem?
0
 
gowflowConnect With a Mentor Commented:
well for sure when it comes to macro and items that we need to dissect then we need to be clear on separators.

Not so quick on changing anything I have an other question:

Are all your store number NUMERIC ??? or it is possible that some could be alphanumeric ?
gowflow
0
 
Wilder1626Author Commented:
That is a good question!

It can be both. Numeric and alphanumeric.
0
 
aikimarkCommented:
Are we assuming that the store IDs in the Vendor worksheet will already be populated or does the code need to parse the storeIDs and populate the other list (vendors-by-store)?
0
 
aikimarkCommented:
Do the storeIDs need to be sorted?

What happened with 0040407?
0
 
Wilder1626Author Commented:
in the Vendor sheet, Stores would already be populated.

I don't need to get a store list sorted unless it is a crucial thing for the macro.

Not for 0040407, you see that we also have the store 4040.  Part of 4040 show in 0040407. But this is 2 different store location IDs. It cannot be mixed together.
0
 
aikimarkCommented:
I already coded this to parse the store values and populate them.  For testing purposes, I created a Test worksheet.
Public Sub ParseStores()
    Dim rngSrc As Range
    Dim rngTgt As Range
    Dim wksSrc As Worksheet
    Dim wksTgt As Worksheet
    Dim oRE As Object
    Dim oMatches As Object
    Dim oMatch As Object
    Dim dicUnique As Object
    Dim vItem As Variant
    
    Set dicUnique = CreateObject("scripting.dictionary")
    dicUnique.comparemode = VbCompareMethod.vbTextCompare
    
    Set oRE = CreateObject("vbscript.regexp")
    oRE.Global = True
    oRE.Pattern = "\b(\w+)\b"
    
    Set wksSrc = Worksheets("Vendor")
    
    For Each rngSrc In wksSrc.Range(wksSrc.Range("C2"), wksSrc.Range("C2").End(xlDown))
        Set oMatches = oRE.Execute(rngSrc.Value)
        Debug.Print oMatches.Count, rngSrc.Value
        For Each oMatch In oMatches
            If dicUnique.exists(oMatch.Value) Then
                dicUnique(oMatch.Value) = dicUnique(oMatch.Value) & ", " & rngSrc.Offset(0, -2).Value
            Else
                dicUnique.Add oMatch.Value, rngSrc.Offset(0, -2).Value
            End If
        Next
    Next
    
    Set wksTgt = Worksheets("Test")
    Set rngTgt = wksTgt.Range("A2")
    wksTgt.Range(rngTgt, rngTgt.Offset(dicUnique.Count - 1)).Value = Application.WorksheetFunction.Transpose(dicUnique.keys)
    Set rngTgt = rngTgt.Offset(0, 2)
    wksTgt.Range(rngTgt, rngTgt.Offset(dicUnique.Count - 1)).Value = Application.WorksheetFunction.Transpose(dicUnique.items)

End Sub

Open in new window

0
 
Wilder1626Author Commented:
This is perfect. Thanks again for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.