Slow Function help please

insanity82007

Board Regular
Joined
Oct 10, 2007
Messages
130
I built this function to find the last character in a string primarily for file renaming so that it would find the last "\" in a directory tree so that I could extract the file name from the list using a mid function after that.

When I run it for lots of rows it slows down heaps and takes ages to calc. Any ideas on how I can improve this?

Cheers

Function FindLast(ByVal inpt As String, what As String) As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

For i = 1 To Len(inpt)
If Mid(inpt, i, Len(what)) = what Then
FindLast = i
End If
Next i


Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic


End Function
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Code:
Function FindLast(ByVal inpt As String, what As String) As Long
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    For i = 1 To Len(inpt)
        If Mid(inpt, i, Len(what)) = what Then
            FindLast = i
        End If
    Next i
   
    
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Function
 
Upvote 0
Maybe this:

Code:
Function FindLast(ByVal sInp As String, sWhat As String) As Long
    FindLast = InStrRev(sInp, sWhat, vbTextCompare)
End Function
 
Upvote 0
I don't think screen update and autocalcultae will speed up the execution of your function because it is not placing values in cells or causing any processes to calculate.. I could be mistaken though. This way may be faster...

Code:
Function getFileName(filePath As String) As String
    Dim pathSplit As Variant
 
    pathSplit = Split(filePath, "\")
    getFileName = pathSplit(UBound(pathSplit))
 
End Function

Untested, but should work.

*Edit - Just saw SHG's suggestion above. Was unaware of InStrRev, definitely the method I would use.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

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