VBA - Using the output from one array to return a value from another array

Mike UK

New Member
Joined
Dec 14, 2011
Messages
41
I have a large array of around 15,000 lines. Each output row contains two fields with a meaningless code. I have two mapping tables to translate these codes (see mocked up idea below). The mapping tables need to be contained within the VBA code rather than a separate sheet using VLookup. I could create two arrays and get the code to loop through each mapping table to replace the code with the name but with 15,000 lines within the main array is this the most efficient way? So for example output row 5623 from the main array could have store code E in one field and Code A02 but I need to replace with meaningful names.

StoreNameCodeFruit
ABrowns GrocersA01Orange
BTescoA02Apple
CSainsburyA03Banana
DAldiA04Grapes
EWaitroseA05Strawberries
FAsdaA06Kiwi
GOcadoA07Raspberries
HMarks & Spencer
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How many stores & codes do you have?
 
Upvote 0
With that amount I would suggest putting the values on a sheet, that can then be pulled into 2 arrays, rather than hard coding them in VBA. It will also make it easier to maintain if there are any changes.
Is that something you can live with?
 
Upvote 0
With that amount I would suggest putting the values on a sheet, that can then be pulled into 2 arrays, rather than hard coding them in VBA. It will also make it easier to maintain if there are any changes.
Is that something you can live with?


Thanks Fluff for the prompt response.
I can put them in a separate sheet. If I do this is it easier to use VLookup or pull the values into 2 arrays and loop through each array to replace the code? Or is there a better VBA solution rather than the loop through each of the two arrays?
 
Upvote 0
With your lists like
Book1
ABCDEF
1StoreNameCodeFruit
2ABrowns GrocersA01Orange
3BTescoA02Apple
4CSainsburyA03Banana
5DAldiA04Grapes
6EWaitroseA05Strawberries
7FAsdaA06Kiwi
8GOcadoA07Raspberries
9HMarks & Spencer
Lists


You could use something like
VBA Code:
Sub MikeUK()
    Dim StoreAry As Variant, CodeAry As Variant
    Dim i As Long
    
    StoreAry = Sheets("Lists").Range("A1").CurrentRegion.Value2
    CodeAry = Sheets("Lists").Range("E1").CurrentRegion.Value2
    
    With Sheets("Data")
        For i = 2 To UBound(StoreAry)
            .Range("A:A").Replace StoreAry(i, 1), StoreAry(i, 2), xlWhole, , False, , False, False
        Next i
        For i = 2 To UBound(CodeAry)
            .Range("H:H").Replace CodeAry(i, 1), CodeAry(i, 2), xlWhole, , False, , False, False
        Next i
    End With
End Sub
Change sheet names & ranges to suit
 
Upvote 0
With your lists like
Book1
ABCDEF
1StoreNameCodeFruit
2ABrowns GrocersA01Orange
3BTescoA02Apple
4CSainsburyA03Banana
5DAldiA04Grapes
6EWaitroseA05Strawberries
7FAsdaA06Kiwi
8GOcadoA07Raspberries
9HMarks & Spencer
Lists


You could use something like
VBA Code:
Sub MikeUK()
    Dim StoreAry As Variant, CodeAry As Variant
    Dim i As Long
   
    StoreAry = Sheets("Lists").Range("A1").CurrentRegion.Value2
    CodeAry = Sheets("Lists").Range("E1").CurrentRegion.Value2
   
    With Sheets("Data")
        For i = 2 To UBound(StoreAry)
            .Range("A:A").Replace StoreAry(i, 1), StoreAry(i, 2), xlWhole, , False, , False, False
        Next i
        For i = 2 To UBound(CodeAry)
            .Range("H:H").Replace CodeAry(i, 1), CodeAry(i, 2), xlWhole, , False, , False, False
        Next i
    End With
End Sub
Change sheet names & ranges to suit


Fluff - many thanks ... that looks quicker going down each column and replacing the values. Excellent thanks for your help.
 
Upvote 0
You're welcome & thanks for the feedback.

One word of caution, if you have stores/codes like A1, A11 make sure that the A11 is higher up the table than the A1 so it gets replaced first
 
Upvote 0
You're welcome & thanks for the feedback.

One word of caution, if you have stores/codes like A1, A11 make sure that the A11 is higher up the table than the A1 so it gets replaced first

OK Great idea, that will save it looping through the whole array. Thanks again Fluff
 
Upvote 0
It will still loop the whole array but if
A1=Tesco and A11 =Ocado
If it replaces A1 before A11 and you had data like Col A
Book1
ABC
1Should beResult
2A1TescoTesco
3A11OcadoTesco1
Data
 
Upvote 0

Forum statistics

Threads
1,215,717
Messages
6,126,422
Members
449,314
Latest member
MrSabo83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top