VBA Vlookup from diffrent workbooks

Sotos13

New Member
Joined
Mar 8, 2019
Messages
42
hello everyone

i have a table that has multiple columns which each cell has this formula. It starts from D2 all the way down to D500
VLOOKUP($C2;'[Test1]Sheet1'!$A$2:$AI$137;4;0)*VLOOKUP($A2;'[Test2]Sheet1'!$A$3:$O$7927;2;0).

so it goes underneath VLOOKUP($C3 ...* VLOOKUP($A3...

In E2 i have the same formula but the only thing that changes is the column index fro both Vlookup
VLOOKUP($C2;'[Test1]Sheet1'!$A$2:$AI$137;6;0)*VLOOKUP($A2;'[Test2]Sheet1'!$A$3:$O$7927;3;0).

and
F2 the same with difrent col.index
VLOOKUP($C2;'[Test1]Sheet1'!$A$2:$AI$137;8;0)*VLOOKUP($A2;'[Test2]Sheet1'!$A$3:$O$7927;4;0).

So the first vlookup column index is changing in a step 2 manner and the second one adding one by one column.
this goes up to Q2
VLOOKUP($C2;'[Test1]Sheet1'!$A$2:$AI$137;30;0)*VLOOKUP($A2;'[Test2]Sheet1'!$A$3:$O$7927;15;0).

At the end in cell R2 i sum C2:Q2, R3 =sum C3:Q3..and so on.

What i want is a vba to calculate the above and just bring back the sum of this multiplication between those two vlookup.

Any ideas or any workarounds?
Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I would do the whole thing in vBA just by using variant arrays which should be much faster, ( and simpler to my mind):
this code should get you started. I wasn't quite sure of what equations you want where but this does one equation and the others are very similar.
Code:
Sub test()
Aname = ActiveWorkbook.Name


Workbooks("Test1").Activate
' note I load the array from A1 to make the indexing simpler
test1array = Worksheets("Sheet1").Range("$A$1:$AI$137")
Workbooks("Test2").Activate
' ditto
test2Array = Worksheets("Sheet1").Range("$A$1:$O$7927")
Workbooks(Aname).Activate
Searcharr = Range("A1:C500")
For i = 2 To 500
  For j = 2 To 137
   If Searcharr(i, 3) = test1array(j, 1) Then
    ' found the first index
      For k = 1 To 7927
           If Searcharr(i, 1) = test2Array(k, 1) Then
            ' found the second index
            ' so do the calculatiosn here
            ' this just does the first equation on your line but the others are very similar
            Cells(i, 4) = test1array(j, 4) * test2Array(k, 2)
           End If
      Next k
   End If
 Next j
Next i
End Sub
 
Upvote 0
Hi Offfthelip,

thanks you for your reply...i will try it to see how it works...so your not using application.formula= Vlookup? it is slower?
 
Upvote 0
Hi Offthelip,

i get a rut time error 9 " subscript out of range' . ihave three workbooks Test1, Test2, Test3 (Test3 is the one i want to have my answer from Test1 and Test2

Also, should we declare the arrays? i think there is where i get the error..

Sub test()
Test3 = ActiveWorkbook.Name


Workbooks("Test1").Activate
' note I load the array from A1 to make the indexing simpler
test1array = Worksheets("Sheet1").Range("$A$1:$AI$137")
Workbooks("Test2").Activate
' ditto
test2Array = Worksheets("Sheet1").Range("$A$1:$O$7927")
Workbooks("Test3").Activate
Searcharr = Range("A1:C500")
For i = 2 To 500
For j = 2 To 137
If Searcharr(i, 3) = test1array(j, 1) Then
' found the first index
For k = 1 To 7927
If Searcharr(i, 1) = test2Array(k, 1) Then
' found the second index
' so do the calculatiosn here
' this just does the first equation on your line but the others are very similar
Cells(i, 4) = test1array(j, 4) * test2Array(k, 2)
End If
Next k
End If
Next j
Next i
End Sub
 
Last edited:
Upvote 0
I presume it is this line:
Code:
[COLOR=#333333][FONT=Verdana]test1array = Worksheets("Sheet1").Range("$A$1:$AI$137"),
[/FONT][/COLOR]
Is the worksheet with the data on it named "Sheet1" because it looks like it isn't
Similar with the others


You don't need to declare the variables unless you have "option explicit" at the top. ( I never do!!)
Excel defaults to declaring anything as Variant. If you load a range from the worksheet into a variable it will automatically defualt to being a variant array
I never declare anything unless it is necessary. I think it makes my code much easier to read because it doesn't start with lines and lines of delcaration which tell you nothing. Spending the time saved writng comments is much more useful I think.
It also means if I have declared something it is for a specific reason so It stand out. e.g this number is double because I need the precision,
 
Last edited:
Upvote 0
No, all the sheets for each workbook is named Sheet1.
i tried changing it with ActiveWorkbook.Sheets("Sheet1").Range...instead but again the same error
 
Upvote 0
I have just tested this code and it correctly picks up the data from my workbook and sheet number. if you are getting an error it is because you have spelt something incorrectly
Code:
Workbooks("BB_set list.xlsx").Activate
' note I load the array from A1 to make the indexing simpler
test1array = Worksheets("Sheet2").Range("$A$1:$AI$137")
MsgBox (test1array(1, 1))
 
Last edited:
Upvote 0
Another quick thought I presume you have got TEST1 and TEST2 open!!
 
Upvote 0
hi ,
i'v tried it with open and with closed the workbooks but again i get the same error.. I changed my workbook names to First, Second, Third and also in the code.Let me show you what i have in each workbook.
In workbook "First" i have this

a157634518126059595959585858
b260665463606059595959585858
c36314763666659514436292114395
d466972147150181211242272303334364395
e514713819150181211242272303334364395
f69144510283135384245485255
g71015547294117139162184207229252274

<colgroup><col width="64" span="15" style="width:48pt"> </colgroup><tbody>
</tbody>


In the workbook "Second" i have

6226713579111315171921232527
6226826101418224815281928482548
6226939152827335115561956512851
62286412205636445460541954545654
62287515255445555765572857575457
62319618305754666070605660605760
62325721356063776375635463636063
62327824406372886680665766666366
6234092745668199568514760147576654
6234810305014790110549096396014757

<colgroup><col width="64" span="15" style="width:48pt"> </colgroup><tbody>
</tbody>

And in workbook "Third" where i also have the code is

622671a
622682b
622693c
622864d
622875e
623196f
623257g
623278h
623409i
6234810j
6237211k
6238112l
6238213m
6238314n
6238415o
6240016p
6242317q
6242418r
6242519s
6242620t
6242721u
6242822v

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Can you post your code, knowing what is in the workbooks doesn't help
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

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