For Each Loop/Lookup

mtngmedic

New Member
Joined
Dec 9, 2009
Messages
2
I am trying to look thru different worksheets for certain values. A list of those worksheets is contained in Range (B1:B15). When I do the lookup outside of the loop structure it works fine I displayed i and the program accurately identified it any suggestions?

For Each i In Range("B1:B15")

Range("D5") = i

c = Application.WorksheetFunction.Lookup(5, ActiveWorkbook.Worksheets(i).Range("AG3:AG100"), ActiveWorkbook.Worksheets(i).Range("AE3:AE100"))

Range("D3") = c


Next i

Thank You!!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I'm just a beginner, but the following seems to work...

Code:
Sub MultiSheetLookup()

Dim MyArray() As String
Dim i As Long
Dim LastRow As Long

LastRow = Cells(Rows.Count, 2).End(xlUp).Row
Sheetcount = Range(Cells(1, 2), Cells(LastRow, 2)).Rows.Count
ReDim MyArray(1 To Sheetcount)

For i = 1 To Sheetcount
    MyArray(i) = Cells(i, 2)
Next i

On Error Resume Next
For i = 1 To Sheetcount
    c = WorksheetFunction.Index(Worksheets(MyArray(i)).Range("AE3:AE100"), _
        WorksheetFunction.Match(5, Worksheets(MyArray(i)).Range("AG3:AG100"), 0))
    If Err.Number = 0 Then Exit For
Next i

Range("D3") = c

End Sub
 
Upvote 0
Hi & Welcome to the Board!!

Try

Code:
Sub Test()
    Dim Rng As Range, LookupRange As Range, ResultRange As Range, c As Variant
    For Each Rng In Range("B1:B5").Cells
        [COLOR=Red]Range("D5").Value = Rng.Value[/COLOR]
        Set LookupRange = Sheets(Rng.Value).Range("AG3:AG100")
        Set ResultRange = Sheets(Rng.Value).Range("AE3:AE100")
        c = Application.WorksheetFunction.Lookup(5, LookupRange, ResultRange)
        [COLOR=Red]Range("D3").Value = c[/COLOR]
    Next Rng
End Sub

I'm not sure of the purpose of the parts in red and hence I've left them as is.
<input id="gwProxy" type="hidden"><!--Session data--><input *******="jsCall();" id="jsProxy" type="hidden">
 
Upvote 0
mtngmedic,

If the intent is to return an approximate match when no exact match exists, replace...

Code:
c = WorksheetFunction.Index(Worksheets(MyArray(i)).Range("AE3:AE100"), _
        WorksheetFunction.Match(5, Worksheets(MyArray(i)).Range("AG3:AG100"), 0))
with

Code:
c = WorksheetFunction.Lookup(5, Worksheets(MyArray(i)).Range("AG3:AG100"), _
        Worksheets(MyArray(i)).Range("AE3:AE100"))
sandeep.warrier,

I tried your code and it returns the following error...

"Run-time error 1004:

Method 'Lookup' of Object 'Worksheetfunction' failed"

When I click on 'Debug', it highlights the following line of code...

Code:
        c = Application.WorksheetFunction.Lookup(5, LookupRange, ResultRange)
Any thoughts?
 
Upvote 0
Hi

I set up a sample workbook as seen below:
Excel Workbook
B
1Sheet2
2Sheet3
Sheet1
Excel 2003
Excel Workbook
AB
11A
22B
33C
44D
55E
66F
77G
88H
99I
1010J
Sheet2
Excel 2003
Excel Workbook
AB
11#
22E
33
44`
55\
66X
77$
88%
99
1010A
Sheet3
Excel 2003

and used the code (modified to suit ranges)

Code:
Sub Test()
    Dim Rng As Range, LookupRange As Range, ResultRange As Range, c As Variant
    For Each Rng In Range("B1:B2").Cells
        Set LookupRange = Sheets(Rng.Value).Range("A1:A10")
        Set ResultRange = Sheets(Rng.Value).Range("B1:B10")
        c = Application.WorksheetFunction.Lookup(5, LookupRange, ResultRange)
        MsgBox c
    Next Rng
End Sub

It worked just fine.

Is your data structured like the example? If not then please let us know how your data is structured.<input id="gwProxy" type="hidden"><!--Session data--><input *******="jsCall();" id="jsProxy" type="hidden">
 
Upvote 0
Thank you all very much for not only your skill but the the speed with which you applied it! You have helped me very much!!!:biggrin::bow::pray:
 
Upvote 0

Forum statistics

Threads
1,223,929
Messages
6,175,449
Members
452,642
Latest member
acarrigan

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