の各ブックを通じてI列でExcelワークブックファイルのパスとファイル名を持つブックを持っている:ループ範囲
C:\D\Folder1\File1.xls
C:\D\Folder2\File2.xls
C:\D\Folder3\File3.xls
各ファイルとそのファイルパスは、上記のようなディレクトリから引き出されます。
これらのワークブックの各
は、私はそうのような私のワークブックの隣接するセルにコピー&ペーストしたいセルC15にメールアドレスが含まれていますC:D\\Folder1\File1.xls [email protected]
C:\D\Folder2\File2.xls [email protected]
C:\D\Folder3\File3.xls [email protected]
私のコードは、唯一のワークブックをチェックし、1つの電子メールアドレスをつかむ
セルD17内:
C:\D\Folder1\File1.xls [email protected]
C:\D\Folder2\File2.xls
C:\D\Folder3\File3.xls
リスト内の各ブックをループする方法を教えてください。
は、ここに私のコードです:
Sub SO()
Dim parentFolder As String
parentFolder = Range("F11").Value & "\" '// change as required, keep trailing slash
Dim results As String
results = CreateObject("WScript.Shell").Exec("CMD /C DIR """ & parentFolder & "*.*"" /S /B /A:-D").StdOut.ReadAll
Debug.Print results
'// uncomment to dump results into column A of spreadsheet instead:
Range("D17").Resize(UBound(Split(results, vbCrLf)), 1).Value = WorksheetFunction.Transpose(Split(results, vbCrLf))
Range("Z17").Resize(UBound(Split(results, vbCrLf)), 1).Value = "Remove"
'//-----------------------------------------------------------------
'// uncomment to filter certain files from results.
'// Const filterType As String = "*.exe"
'// Dim filterResults As String
'//
'// filterResults = Join(Filter(Split(results, vbCrLf), filterType), vbCrLf)
'//
'// Debug.Print filterResults
On Error GoTo errHandler
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim app As New Excel.Application
app.Visible = False 'Visible is False by default, so this isn't necessary
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open(Range("D17").Value)
Set y = ThisWorkbook
'Now, copy what you want from x:
x.Worksheets(1).Range("C15").Copy
'Now, paste to y worksheet:
y.Worksheets(1).Range("U17").PasteSpecial xlPasteValues
'Close x:
x.Close
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
errHandler:
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
End Sub
最初に、Dir $はVBAに存在する関数であり、複雑な呼び出しをwscriptに使用する必要はありません。 –
第2に、エラー処理が良くありません。 –