私はこれらのブックに含まれているExcelファイルパスと電子メールアドレスのリストを生成する次のコードを持っています。私は、次のちょうど生産するために私のファイルパスをトリミングすることができますどのように文字列/ファイルパスをvbaでトリミングしますか?
G:\folder1\file.xls [email protected]
::これはそうのような結果が生成さ
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
:
file.xls [email protected]
私が試してみましたコードは
replace(pathsEmails(fle.path), "G:\folder1\" , "")
しかし、これは動作しません。誰かが私がどこに間違っているのかを教えてもらえますか?
編集:
時々私は、セルC15で複数のメールアドレスを持っています。
[email protected]/[email protected]
これは、ブック内のメールはそうのように記載されていることが発生します
[email protected]/[email protected]
は私が/
を交換し、(それは優しい電子メールにする),
に置き換えることができ、とにかくあり
をあなたの結果は、2列(GとV)でoutputedているように見えます、私は正しい?ファイルの完全なパスが不要で、ファイルの名前が同じでない場合は、ファイル名を辞書の 'pathsEmails(fle.name)= book.Worksheets(1).Rangeのキーとして使用できます( "C15")。値。または、 "\" – R3uK
の前にすべてを削除する列をループします。@ R3uKはい列Vに電子メールが含まれ、列Gにブックファイルのパスが含まれます。 – user7415328
OK!あなたは時々同じファイルの名前を持っていますか?また、あなたが投稿したコード以外のファイルの完全なパスを使用する必要がありますか? – R3uK