Solved

VBA - Excel Vlookup between 2 sheets

Posted on 2014-02-12
11
714 Views
Last Modified: 2014-02-12
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
0
Comment
Question by:Wilder1626
  • 5
  • 4
  • 2
11 Comments
 
LVL 29

Expert Comment

by:gowflow
ID: 39853158
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
 
LVL 11

Author Comment

by:Wilder1626
ID: 39853162
90% of them yes, they are separated by a comma. I guess i may need to put comma everywhere. Do i?
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39853197
So 90% of them have comma, but 100% have a space between them.

Can it be a problem?
0
 
LVL 29

Assisted Solution

by:gowflow
gowflow earned 100 total points
ID: 39853246
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
 
LVL 11

Author Comment

by:Wilder1626
ID: 39853254
That is a good question!

It can be both. Numeric and alphanumeric.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 45

Expert Comment

by:aikimark
ID: 39853275
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
 
LVL 45

Expert Comment

by:aikimark
ID: 39853296
Do the storeIDs need to be sorted?

What happened with 0040407?
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39853352
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
 
LVL 45

Expert Comment

by:aikimark
ID: 39853391
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
 
LVL 45

Accepted Solution

by:
aikimark earned 400 total points
ID: 39853413
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
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 39853602
This is perfect. Thanks again for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now