BBlu
asked on
Still trying to figure out how to skip over code using Goto Statement
This post is related to:
https://www.experts-exchange.com/questions/26913245/Best-way-to-skip-over-code-with-Goto.html
I thought we'd solved the problem, but it seems that it's doing the second block of code (lines 11-24) regardless.
Any suggestions on getting the function to end if both of the parameters retrieved are null?
https://www.experts-exchange.com/questions/26913245/Best-way-to-skip-over-code-with-Goto.html
I thought we'd solved the problem, but it seems that it's doing the second block of code (lines 11-24) regardless.
Function FindDept(AccountRef1, AccountRef2)
Dim colonPos As Integer
Dim AccountRefToUse As String
Dim TextToRight As String
If IsNull([AccountRef1]) And IsNull([AccountRef2]) Then
FindDept = ""
GoTo EndofSection
End If
If AccountRef1 = "" Then
AccountRefToUse = AccountRef2
Else
AccountRefToUse = AccountRef1
End If
colonPos = InStr([AccountRefToUse], ":")
TextToRight = Right([AccountRefToUse], Len([AccountRefToUse]) - colonPos)
If colonPos = 0 Then
FindDept = [AccountRefToUse]
Else
FindDept = TextToRight
End If
EndofSection:
End Function
Any suggestions on getting the function to end if both of the parameters retrieved are null?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
use "Exit Function" instead of "Goto EndOfSection" to exit inmediately
if the function dowes not exits you maybe forgetting something in the
main condition "If IsNull([AccountRef1]) And IsNull([AccountRef2]) Then...."
if the function dowes not exits you maybe forgetting something in the
main condition "If IsNull([AccountRef1]) And IsNull([AccountRef2]) Then...."
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have reduced your function to just few lines. Could you test this for me?
Sid
Function FindDept(AccountRef1, AccountRef2) As String
Dim colonPos As Integer
AccountRefToUse As String, TextToRight As String
If IsNull([AccountRef1]) And IsNull([AccountRef2]) Then Exit Function
If IsNull([AccountRef1]) Then [AccountRefToUse] = [AccountRef2] Else [AccountRefToUse] = [AccountRef1]
colonPos = InStr([AccountRefToUse], ":")
TextToRight = Right([AccountRefToUse], Len([AccountRefToUse]) - colonPos)
If colonPos = 0 Then FindDept = [AccountRefToUse] Else FindDept = TextToRight
End Function
Sid
Also one more thing. If there is no ":" in [AccountRefToUse] then you might incorrect results because of this line of code
colonPos = InStr([AccountRefToUse], ":")
If AccountRef1 and AccountRef2 are string then you can use this code as well.
Sid
colonPos = InStr([AccountRefToUse], ":")
If AccountRef1 and AccountRef2 are string then you can use this code as well.
Function FindDept(AccountRef1 As String, AccountRef2 As String) As String
Dim colonPos As Integer
AccountRefToUse As String, TextToRight As String
If Len(Trim(AccountRef1)) = 0 And Len(Trim(AccountRef2)) = 0 Then Exit Function
If Len(Trim(AccountRef1)) = 0 Then AccountRefToUse = AccountRef2 Else AccountRefToUse = AccountRef1
colonPos = InStr(1, AccountRefToUse, ":")
TextToRight = Right(AccountRefToUse, Len(AccountRefToUse) - colonPos)
If colonPos = 0 Then FindDept = AccountRefToUse Else FindDept = TextToRight
End Function
Sid
ASKER
Thanks to you both.
It was the exclusion of the "Or" that I was missing. I'd actually thought of that before fraigor mentioned it. I guess that means I'm learning. LOL. But you guys did teach a few more things (Exit Function, using as, shortening the code) Thank you both!
ASKER
Oh, here is the final code that works:
Function FindDept(AccountRef1, AccountRef2)
Dim colonPos As Integer
Dim AccountRefToUse As String
Dim TextToRight As String
If (IsNull([AccountRef1]) Or [AccountRef1] = "") And (IsNull([AccountRef2]) Or [AccountRef2] = "") Then
FindDept = ""
Exit Function
End If
If AccountRef1 = "" Then
AccountRefToUse = AccountRef2
Else
AccountRefToUse = AccountRef1
End If
colonPos = InStr([AccountRefToUse], ":")
TextToRight = Right([AccountRefToUse], Len([AccountRefToUse]) - colonPos)
If colonPos = 0 Then
FindDept = [AccountRefToUse]
Else
FindDept = TextToRight
End If
EndofSection:
End Function
BBlu: Please refer to ID: 35221406
Sid
Sid
ASKER
Thanks, Guys
BBlu,
Glad you got your answer. Just so you know, use of GoTo is very much discouraged, except for invoking an error handler. The other Experts did not come out and say this, but do note how their suggestions avoid the need for GoTo by using the typical flow-of-control structures VBA provides. As such, they are implicitly leading you away from GoTo :)
Patrick
Glad you got your answer. Just so you know, use of GoTo is very much discouraged, except for invoking an error handler. The other Experts did not come out and say this, but do note how their suggestions avoid the need for GoTo by using the typical flow-of-control structures VBA provides. As such, they are implicitly leading you away from GoTo :)
Patrick
ASKER
Got it. Thank you very much, Patrick. Have a great weekend.
ASKER
If AccountRef1 = "" Then
AccountRefToUse = AccountRef2
It must be still executing this part even when one of the values is null