Combining two arrays in VBA?

NiicckkM

New Member
Joined
Jul 9, 2015
Messages
28
I have an array that is looping:

Code:
Sub Test()
Dim FileLoop As Variant, Found As Range

For Each FileLoop In Array ("A", "B", "C", "D", "E") 

If FileLoop = [A1] then
MsgBox FileLoop

Next Fileloop

I need to create a new variable, based on the array value.

A = Alpha
B = Beta
C = Charlie
D = Delta
E = Echo

The only way I know how to assign this is below:

Code:
If FileLoop = A then FileLoop2 = Alpha
If FileLoop = B then FileLoop2 = Beta
If FileLoop = C then FileLoop2 = Charlie
If FileLoop = D then FileLoop2 = Delta
If FileLoop = E then FileLoop2 = Echo

I was hoping to find a way to do this that uses less lines of code. If I create a new array (like below), would I be able to assign values based on the locations in each array?

First Array: ("A", "B", "C", "D", "E")
Second Array: ("Alpha", "Beta", "Charlie", "Delta", "Echo")



Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
There are several ways to accomplish this. Your idea could work like this:

Rich (BB code):
MyArray1 = Array("A", "B", "C", "D", "E")
MyArray2 = Array("Alpha", "Beta", "Charlie", "Delta", "Echo")

For i = 0 to Ubound(MyArray1)
    FileLoop = MyArray1(i)
    FileLoop2 = MyArray2(i)
Next i


Another way:
Rich (BB code):
' Initialize the arrays
MyArray1 = Array("A", "B", "C", "D", "E")
MyArray2 = Array("Alpha", "Beta", "Charlie", "Delta", "Echo")

' Save the data in a dictionary
Set MyDict = CreateObject("Scripting.Dictionary")

For i = 0 to Ubound(MyArray1)
   MyDict.Add MyArray1(i), MyArray2(i)
Next i

' Now you can pull out the associated item in one line
For Each FileLoop in MyArray1
    FileLoop2 = MyDict.Item(FileLoop)
Next i


There are other ways, but this should give you some ideas.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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