Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

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
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

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
Avatar of Wilder1626

ASKER

90% of them yes, they are separated by a comma. I guess i may need to put comma everywhere. Do i?
So 90% of them have comma, but 100% have a space between them.

Can it be a problem?
SOLUTION
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is a good question!

It can be both. Numeric and alphanumeric.
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)?
Do the storeIDs need to be sorted?

What happened with 0040407?
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.
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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is perfect. Thanks again for your help.