我正在嘗試在網路驅動器中搜索包含兩個關鍵字的檔案。找到后,我需要他們將所述檔案的最后修改日期回傳到從中提取關鍵字之一的同一行。我找到了與我需要的類似的東西,但它不搜索特定的關鍵字。
Sub GetFilesDetails()
' in column G= Date Last Modified
Dim objFSO As Scripting.FileSystemObject
Dim myFolder As Scripting.Folder
Dim myFile As Scripting.File
Dim R as Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set myFolder = objFSO.GetFolder(“S:\”)
Application.ScreenUpdating = False
For Each myFile In myFolder.Files
ThisWorkbook.Sheets("Sheet1").Cells(R, 7).Value = myFile.DateLastModified
R = R 1
Next myFile
Application.ScreenUpdating = True
MsgBox "Updated"
End Sub
我需要關鍵字 1 是“證明”,關鍵字 2 是基于 B 列值的變數。因此從第 4 行開始,關鍵字“證明”和 (B4) 是查找最新檔案的搜索詞,并將檔案的最后修改日期回傳到 (G4)。從那里繼續執行相同任務的行,但跳過任何帶有空白 B 單元格的行。
任何幫助是極大的贊賞!
編輯:關鍵字將在檔案名中。即“WO67547_Proof1”證明在我將要查找的所有檔案上,WO# 是變數。只要提取的唯一最后修改日期是最近的,WO# 和 Proof 關鍵字的出現次數不應超過一次。

uj5u.com熱心網友回復:
請使用下一個代碼。它提取與兩個關鍵字中的每對匹配的匹配檔案名,并選擇最近的日期。代碼應該非常快,使用陣列。對于處理和回傳,也是:
Sub GetFilesDetails()
Dim sh As Worksheet, lastR As Long, arrKeys, arrDate, i As Long, fileName As String
Dim folderPath As String, lastModifDate As Date, lastDate As Date
Const key2 As String = "Proof"
Set sh = ActiveSheet 'use here the necessary worksheet
lastR = sh.Range("B" & sh.rows.count).End(xlUp).Row
arrKeys = sh.Range("B4:B" & lastR).Value2 'place the range in an array for faster iteration
arrDate = sh.Range("G4:G" & lastR).Value2
folderPath = "C:/the necessary folder path" 'Use here your real Folder Path!!!
For i = 1 To UBound(arrKeys)
If arrKeys(i, 1) <> "" Then
fileName = Dir(folderPath & "\" & "*" & arrKeys(i, 1) & "*" & key2 & "*.xlsx")
lastDate = 0
Do While fileName <> ""
lastModifDate = CDate(Int(FileDateTime(folderPath & "\" & fileName)))
If lastModifDate > lastDate Then lastDate = lastModifDate
fileName = Dir
Loop
If lastModifDate <> 0 Then arrDate(i, 1) = lastModifDate: lastModifDate = 0
End If
Next i
With sh.Range("G4").Resize(UBound(arrDate), 1)
.Value2 = arrDate
.NumberFormat = "dd-mmm-yy"
End With
End Sub
不要忘記folderPath使用要處理的檔案所在的真實檔案夾進行更新。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/484819.html
