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

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
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,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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