Solved

VBA - Excel Vlookup between 2 sheets

Posted on 2014-02-12
11
1,289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 31

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 31

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
 
LVL 46

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 46

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 46

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 46

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

627 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