View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Rbp9ad[_2_] Rbp9ad[_2_] is offline
external usenet poster
 
Posts: 25
Default Trouble with looping

I am trying to write a function that loops so that I can use vlookup in
multiple ranges. The table arrays are on different sheets of the same
workbook. There are twelve of them and I have named them the month name
followed by 1(i.e. January1). The loop should go to the next range when it
does not find the value, but currently it does not. It will only calculate
for January. If someone could help that would be great.

Option Explicit
Function RRLookup(VRN As Variant) As Variant
Dim testRng As Range
Dim iCtr As Integer
Dim res As Variant


iCtr = 1
Set testRng = ThisWorkbook.Names(Format(DateSerial(2005, iCtr, 1), "mmmm")
& "1").RefersToRange
res = Application.WorksheetFunction.VLookup(VRN, testRng, 3, False)
Set testRng = Nothing
Do Until IsError(res) = False
iCtr = iCtr + 1
Set testRng = ThisWorkbook.Names(Format(DateSerial(2005, iCtr, 1),
"mmmm") & "1").RefersToRange
res = Application.WorksheetFunction.VLookup(VRN, testRng, 3, False)
Set testRng = Nothing
If iCtr = 13 Then
res = "Not Valid"
End If
Loop
RRLookup = res

End Function