User Defined Function doesn't work

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
852
Hi everybody
I've written the following function to extract two digits from a 14 digits and then use these two digits to return a string value
But it doesn't work as I expect
Code:
Function Governorate(strID As Variant) As String
Dim strGov As String
If Len(strID) = 14 Then
    strGov = Mid(strID, 8, 2)
    If strGov = "33" Then
    Governorate = "Sector1"
    ElseIf strGov = "02" Then
    Governorate = "Sector2"
    Else
    GoTo Err
    End If
End If
Err:
Governorate = ""
End Function

I want to fix it
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try

Code:
Function Governorate(strID As Variant) As String
If Len(strID) = 14 Then
    Select Case Mid(strID, 8, 2)
        Case "33": Governorate = "Sector1"
        Case "02": Governorate = "Sector2"
        Case Else: Governorate = ""
    End Select
End If
End Function
 
Upvote 0
Mr. VoG
Thanks a lot
Your code works fine but I still want to discover the faults in my code to learn from my faults...
 
Upvote 0
With your original code you would need to add the line in red to bypass the error handler

Rich (BB code):
Function Governorate(strID As Variant) As String
Dim strGov As String
If Len(strID) = 14 Then
    strGov = Mid(strID, 8, 2)
    If strGov = "33" Then
    Governorate = "Sector1"
    ElseIf strGov = "02" Then
    Governorate = "Sector2"
    Else
    GoTo Err
    End If
End If
Exit Function
Err:
Governorate = ""
End Function
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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