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
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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