I'm trying to change a subroutine that I've been using for a while into a function, so that it can be called directly rather than from a separate subroutine. Here's what I have at the moment:
Function FindCode(strPhoneNum As String) As String
'Sorts through the data in the Master Inventory that may contain a phone number; when it finds the
'number it's looking for, it returns the first 7 digits of the "account name" as the coding string,
'and exits the function. If it doesn't find anything, it requests user input from another source.
'Set Object References
If IsRepOpen("CL Master Long Distance Inventory 2012.xls") = False Then
Workbooks.Open Filename:="\\files1\TelcomPublicShare\CL Master Long Distance Inventory 2012.xls", ReadOnly:=True
End If
Set wbTelcom = Workbooks("\\files1\TelcomPublicShare\CL Master Long Distance Inventory 2012.xls")
Set wsList = wbTelcom.Worksheets("Outbound") 'Identify tab with number inventory.
'Validate Phone Number
Do While strPhoneNum Like "*[A-Z]*" Or Len(strPhoneNum) <> 10
strPhoneNum = InputBox(prompt:="Phone number should only be 10 digits long and" & vbCr & _
"include no alpha characters or punctuation. Please input valid number:" & vbCr & _
"Example: 1234567890", title:="Invalid Phone Number", Default:="XXXXXXXXXX")
Loop
wsList.Activate
'Find the phone number in the Master Inventory; request valid billing code if number is not in inventory.
On Error GoTo ErrorHandler
FindCode = Left(Cells(2, Cells.Find(strPhoneNum).Column), 7)
Application.ScreenUpdating = True
ThisWorkbook.Activate
Exit Function
ErrorHandler:
Application.ScreenUpdating = True
FindCode = InputBox(strPhoneNum & " is not in the" & vbCr _
& "Master Inventory. Please input" & vbCr _
& "the seven-digit billing code.", "No Valid Code", "XXXXXXX")
End Function
The problem is that when I run the Workbooks.open method from the function, it doesn't do anything. It loads all the property data for the file into wbTelcom, but the file doesn't open up, and the object seems to refer back to ThisWorkbook instead.
This only seems to be a problem if I enter macro processing through the function; if I use a command button that calls a standard subroutine with the method called there, it works fine. Is it some sort of problem with trying to call the method from a function? Or did I screw up the syntax somewhere?
Bookmarks