I have the following code which produces a list of excel file paths and email addresses contained in those workbooks.
Code:
Option Explicit
Sub SO()
'clear the existing list here -- not implemented
'...
Range("G17:G" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row).ClearContents
Range("V17:V" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row).ClearContents
Range("AD17:AD" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row).ClearContents
Dim pathsEmails As New Dictionary
Dim app As New Excel.Application
Dim fso As New FileSystemObject
Dim weekFolder As Folder
'replace 1 with either the name or the index of the worksheet which holds the week folder path
'replace B4 with the address of the cell which holds the week folder path
Set weekFolder = fso.GetFolder(Worksheets(1).Range("I8").Value)
Dim supplierFolder As Folder, fle As file
For Each supplierFolder In weekFolder.SubFolders
For Each fle In supplierFolder.files
'test whether this is an Excel file
If fle.Type Like "*Excel*" Then
'open the workbook, read and save the email, and close the workbook
Dim book As Workbook
On Error Resume Next
Set book = app.Workbooks.Open(fle.path, , True)
pathsEmails(fle.path) = book.Worksheets(1).Range("C15").Value
book.Close False
End If
Next
Next
app.Quit
'copy the paths and emails to the worksheet
'(as above) replace 1 with either the name or the index of the worksheet which holds the week folder path
'paths are pasted in starting at cell B6, downwards
'emails are pasted in starting at cell C6, downwards
Worksheets(1).Range("G17").Resize(UBound(pathsEmails.Keys) + 1, 1).Value = WorksheetFunction.Transpose(pathsEmails.Keys)
Worksheets(1).Range("V17").Resize(UBound(pathsEmails.Items) + 1, 1).Value = WorksheetFunction.Transpose(pathsEmails.Items)
'Clear empty cells
On Error Resume Next
Range("V17:V" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
This produces a result like so:
G:\folder1\file.xls [email protected]
How can i trim my file path to produce just the following:
file.xls [email protected]
I have tried
replace(pathsEmails(fle.path), "G:\folder1\" , "")
But this doesn't work. Please can someone show me where i am going wrong?
Edit:
Sometimes i have more than one email address in the cell C15.
[email protected] / [email protected]
So this causes the emails in the workbook to be listed like so:
[email protected] / [email protected]
Is there anyway i can replace the / and replace it with a , (to make it email friendly)
pathsEmails(fle.name) = book.Worksheets(1).Range("C15").Value. Or loop on the columns to remove everything before the "\" - R3uKReplacefunction. Also, your first question isn't rocket science and you could have found the answer with a little bit of research into the File object, using theNameproperty instead of thePathproperty. - Zev Spitz