VBA & Macro Problems

Extract Text After the Last Slash in a URL Using VBA

Maira MehtabMaira Mehtab Nov 18, 2026 869 views

Question details

Extract the text after the last slash in a URL for existing and new records.

Extract Text After the Last Slash in a URL Using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open a standard module

Open the VBA editor with Alt+F11, then select Insert > Module.

2
Add the function

Paste this code into the module: Public Function GetLastUrlPart(strPath As String) As String GetLastUrlPart = Mid$(strPath, InStrRev(strPath, "/") + 1) End Function

3
Use the function in Access

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.

4
Or use the function in Excel

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.

5
Save the code

Save the Access database or save the Excel workbook as an Excel Macro-Enabled Workbook (.xlsm), then enable macros when opening a trusted file.

Use a VBA function to return the last URL segment
Check URL endings: A URL ending in a slash returns an empty string. Query strings or fragments after the last slash are included in the result. The Access query displays a calculated value; it does not store it in a separate field.
Free Microsoft Office alternative

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.

Work with URL columns and spreadsheet formulas in WPS SpreadsheetsOpen and edit compatible Microsoft Excel formats, including XLSXUse a familiar interface when moving from Microsoft OfficeGet a free, lightweight office suite
microsoft office alternative - wps office

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.