Extract Text After the Last Slash in a URL Using VBA
Question details
Extract the text after the last slash in a URL for existing and new records.

- Product
- Microsoft Access or Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A database field or worksheet column contains URLs, and the final segment is needed as a separate value.
- Observed behavior
- The desired result is the final URL segment, calculated for existing entries and available for new entries.
Identify the URL field or column and open the database or macro-enabled workbook in which you want to use the function. Make sure macros are enabled for trusted files.
Use a VBA function to return the last URL segment
Add a reusable function, then call it from an Access query or an Excel worksheet.
InStrRev finds the last slash, and Mid$ returns the text that follows it. If there is no slash, the function returns the whole input.
Open the VBA editor with Alt+F11, then select Insert > Module.
Paste this code into the module: Public Function GetLastUrlPart(strPath As String) As String GetLastUrlPart = Mid$(strPath, InStrRev(strPath, "/") + 1) End Function
In a query's Design view, add a calculated column such as LastPart: GetLastUrlPart(Nz([URL], "")), replacing URL with your field name. Run the query to calculate the result for existing and new records.
Enter =GetLastUrlPart(A2) in a result cell, replacing A2 with the URL cell, and fill the formula down. For new rows, use an Excel table and a calculated column, or copy the formula into each new row.
Save the Access database or save the Excel workbook as an Excel Macro-Enabled Workbook (.xlsm), then enable macros when opening a trusted file.

Work with URL lists in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar interface for working with spreadsheet files. Open compatible Excel files to continue working with URL lists. WPS is not a Microsoft Access replacement, and VBA compatibility can vary by installation and feature.

Frequently Asked Questions
How do I use the function with an Access field that can be null?
In Access query Design view, use an expression such as LastPart: GetLastUrlPart(Nz([URL], "")) to pass an empty string when the URL field is null.
Will the Access query work for new records?
Yes. When you run the query after adding records, its calculated column evaluates the function for those records too. It does not save the result in a separate field.
What happens if a URL ends with a slash?
The function returns an empty string because there is no text after the last slash. Remove the trailing slash first if you need the preceding segment.
Why does the result include a query string?
The function returns all text after the last slash, including any query string or fragment. It does not parse those URL components separately.




