Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lookup based on all sheets in workbook

Status
Not open for further replies.

krispi

MIS
May 16, 2002
281
GB
All - I am trying to do a lookup based on the same range for all worksheets in a workbook, and return the results to another workbook (one column of results per worksheet). I'm just trying to get some test code to work at the moment, but (based on Watches) it's falling over when it gets to the line 'For Each ws In wb2' - any advice as to why would be gratefully appreciated...
Code:
Private Sub test1()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws As Worksheet
Dim intR As Integer
Dim intC As Integer
Dim LURange As Range
Dim LUValue As String
Dim strVal As String
Dim myVar As Variant

Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("J:\Chris\Automation\Test1.xls")
intC = 2
For Each ws In wb2
     Set LURange = ws.Range("A:B")
        intR = 1
            Do While intR < 4
                LUValue = wb1.Worksheets("Sheet1").Cells(intR, 1)
                myVar = Application.VLookup(LUValue, LURange, 2, False)
                wb1.Worksheets("sheet1").Cells(intR, intC).Value = myVar
                intR = intR + 1
            Loop
        intC = intC + 1
    Next ws
End Sub
 
Dave it's been a long day! Thanks for saving my sanity!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top