I want a VBA code that will list all file names used in my workbook 'Service Providers'…
- User
I want a VBA code that will list all file names used in my workbook 'Service Providers' The file names will end with a file name type, for example .pdf, .docm, .xlsm, .txt The common factor in the file name is the '.' before the file type. The other common factor in the file name is the '' just before the file name. I want the list of all files in column L starting from L2 and the location of file in the workbook in column M
- Assistant
Here is a VBA code that will list all file names with their respective locations in your workbook 'Service Providers'.
Sub ListFileNames() Dim fileName As String Dim filePath As String Dim lastRow As Long ' Clear existing data in column L and M Range("L2:M" & Cells(Rows.Count, "L").End(xlUp).Row).ClearContents ' Starting row for file names lastRow = 2 ' Loop through each shape in the workbook For Each shp In ThisWorkbook.Sheets("Service Providers").Shapes ' Check if the shape is a linked file If shp.Type = msoLinkedPicture Then ' Get the file name and location fileName = Mid(shp.Name, InStrRev(shp.Name, ".") + 1) filePath = Mid(shp.Name, InStrRev(shp.Name, "\") + 1, InStrRev(shp.Name, ".") - InStrRev(shp.Name, "\") - 1) ' Add the file name and location to columns L and M Range("L" & lastRow).Value = fileName Range("M" & lastRow).Value = filePath ' Increment the row number lastRow = lastRow + 1 End If Next shp End Sub