User Defined Function doesn't work

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
833
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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
833
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...
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,920
Messages
5,834,375
Members
430,282
Latest member
MeredithD26

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
Top