Pls explain the below macro.

Vincent paul

New Member
Joined
Oct 8, 2014
Messages
22
Hi,

Im just started to learn macros.
In below code why tempindex assigend as 1 and what is difference between exit function and end function.

Function EGLookup(SearchRange As Range, SearchValue As String, RetreiveRange As Range)
Dim TempIndex As Double
TempIndex = 1
For Each cell In SearchRange
If CStr(LCase(cell.Value)) = CStr(LCase(SearchValue)) Then
EGLookup = RetreiveRange.Rows(TempIndex).Value
Exit Function
End If
TempIndex = TempIndex + 1
Next
End Function
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
Hello,

having only quickly looked at the code, it seems to be some sort of looping code, which starts at Row 1.

Exit Function is there to stop the code during a specific event, in this case, when this occurs

If CStr(LCase(cell.Value)) = CStr(LCase(SearchValue)) Then

End Function is there so that particular function has an end point, if you have more than one function in the code, the program knows where to stop and start. It's basically the natural ending of the code.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
If TempIndex isn't initialised to 1 it will be zero, and the index into RetrieveRange would be 1 less than it should be. Exit Function ends the function if a match is found - there is no point continuing the loop.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,032
Messages
5,526,358
Members
409,697
Latest member
christopherlewis1620

This Week's Hot Topics

Top