Error Handling Custom Functions

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,216
Office Version
  1. 2016
Hi guys! Sorry not here that often but when I need help :(

I got this custom function below it is I guess simple but I am struggling a little ..

Code:
Function LastSlash(strText As String) As String
    Dim intPos As Integer
    intPos = InStrRev(strText, "/")
    LastSlash = Mid(strText, intPos + 1)
End Function

Now as I am using this function in a query there are not always slashes in the string I am looking for.
So how do I modify this function so that it only displays the last string after the slash..
For example... if there is no slash in the text I am looking for just keep it blank.
Currently I am getting an Error ..

Hope someone could give me a help with this!

Many thanks

Albert
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Check the value of intPos.

If it's 0 return a blank.
Code:
If intPos = 0 Then
    LastSlash = ""
Else
    LastSlash = Mid(strText, intPos + 1)
End If
 
Upvote 0
Check the value of intPos.

If it's 0 return a blank.
Code:
If intPos = 0 Then
    LastSlash = ""
Else
    LastSlash = Mid(strText, intPos + 1)
End If

Hi Norie!!
thanks for your input!

Code:
Function LastSlash(strText As String) As String
    Dim intPos As Integer
    If intPos = 0 Then
        LastSlash = ""
    Else
    intPos = InStrRev(strText, "/")
    LastSlash = Mid(strText, intPos + 1)
    End If
End Function

this is how I have it now but unfortunatelly it did not make a difference in the query... :eek:
I thought that would do it for sure ..
 
Upvote 0
OH Sorry Norie,
I changed it to

Code:
Function LastSlash(strText As String) As String
    Dim intPos As Integer
    intPos = InStrRev(strText, "/")
    
    If intPos = 0 Then
        LastSlash = ""
    Else

    LastSlash = Mid(strText, intPos + 1)
    End If
End Function
and it works..
But just one littel adjustment
when it is blank how to change it then? :(
so if it is 0 and or blank

pff so simple in a way ...
 
Upvote 0
If strText is blank intPos will be 0.
 
Upvote 0
Code:
'*****************************************************
Sub test_it()
    
    Dim retval As Variant
    Dim param As Variant
    
    param = Null
    retval = LastSlash(param)
    Debug.Print retval
    
    param = "0123456789"
    retval = LastSlash(param)
    Debug.Print retval
    
    param = "/123456789"
    retval = LastSlash(param)
    Debug.Print retval
    
    param = "012345678/"
    retval = LastSlash(param)
    Debug.Print retval
    
    param = "01234/6789"
    retval = LastSlash(param)
    Debug.Print retval
    
    param = "01/3456/89"
    retval = LastSlash(param)
    Debug.Print retval
        
End Sub
'*****************************************************
Function LastSlash(strText As Variant) As Variant
        
    Dim intPos As Integer
    Dim s As String
    If IsNull(strText) Then
        LastSlash = Null
    Else
        intPos = InStrRev(strText, "/")
        If intPos = 0 Or intPos = Len(strText) Then
            LastSlash = Null
        Else
            LastSlash = Mid(strText, intPos + 1)
        End If
    End If
    
End Function
'*****************************************************
 
Upvote 0
Just a question regarding this function!
I currently using it in a query.. it works fine!
Now is there also a way to update it straight away in the table as a recordset?
I know how to update a recordset as a code but not together with a function... ??

Cheers :)
 
Upvote 0

Forum statistics

Threads
1,221,842
Messages
6,162,333
Members
451,760
Latest member
samue Thon Ajaladin

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