I have a function whose purpose is to take in a string and search the first row of a spreadsheet for cells that contain that string, and if they do it selects that column and assigns its value to a variable. However, if none contain that string I would like it to exit the subroutine completely. I've tried throwing the Exit Sub command within the function, but I keep getting error messages. The function is never called directly by the main sub, but from other functions that are called by the sub. Does anybody have any ideas on how I can get this to work? Here's the code that I have for the function:
Function ColSearch(Heading As String) As Integer
'Determines the column number of the desired heading
Sheets("CS-CRM Raw Data").Select
Sheets("CS-CRM Raw Data").Unprotect
On Error Resume Next
If Sheets("CS-CRM Raw Data").Cells.Find(What:=Heading, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Column > 0 Then
myCol = Sheets("CS-CRM Raw Data").Cells.Find(What:=Heading, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Column
Else
End
End If
ColSearch = myCol
End Function
On Error Resume Nextyou should also ensure to writeOn Error Goto 0orOn Error Goto <MyErrorLabel>somewhere downstream.