我有搜索和複製文件夾中的一些文件從Excel列表,如啓動:VBA搜索字符串,字符串
8100.pdf
100_8152.pdf
102_8153.pdf
8153 (2).pdf
:
8100
8152
8153
有命名這樣的文件的文件夾中
如何在不重命名所有文件的情況下搜索這些文件? 由於user3598756,這是我現在使用與Excel列表相同的名稱和文件夾搜索文件的代碼:
Option Explicit
Sub cerca()
Dim T As Variant
Dim D As Variant
T = VBA.Format(VBA.Time, "hh.mm.ss")
D = VBA.Format(VBA.Date, "yyyy.MM.dd")
Dim Source As String
Dim Dest As String
Dim Missed As String
Dim fileFound As String
Dim CodiceCS As Variant
Dim cell As Range
Source = "D:\myfolder\"
Dest = "D:\myfolder\research " & D & " " & T
If Dir(Dest, vbDirectory) = "" Then MkDir Dest '<--| create destination folder if not alerady there
With Worksheets("Cerca") '<-- reference your worksheet with pdf names
For Each cell In .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants) '<-- loop through its column "A" cells with "constant" (i.e. not resulting from formulas) values from row 2 down to last non empty one
CodiceCS = VBA.Left((cell.Value), 4)
fileFound = Dir(Source & "\" & CodiceCS & "\*" & cell.Value & "*.Pdf") '<-- look for a source folder file whose name contains the current cell value
If fileFound <> "" Then '<-- if found...
FileCopy Source & "\" & CodiceCS & "\" & fileFound, Dest & "\" & fileFound '<-- ...copy to destination folder
Else '<--otherwise...
Missed = Missed & cell.Value & vbCrLf '<--... update missing files list
End If
Next cell
End With
If Missed <> "" Then '<-- if there's any missing file
Dim FF As Long
FF = FreeFile
Open (Dest & "\" & "MissingFiles.txt") For Output As #FF
Write #FF, VBA.Left(Missed, Len(Missed) - 2)
Close #FF
End If
MsgBox "OK"
Shell "explorer.exe " + Dest, vbNormalFocus
End Sub
代碼工作與前綴的所有文件,但不使用的文件後綴(即:「8153(2).pdf」)。代碼只返回一個文件,但我需要所有與單元格值匹配的文件。我需要延長我對多年組織的子文件夾的研究(即:「D:\ myfolder \ 2015」,「D:\ myfolder \ 2016」等)。
看一下函數'InStr' - 它的Wi我會給你所有你需要回答這個問題的。如果您遇到問題,請回來解釋問題,我們可以幫助您將其分類... – Dave
感謝戴夫,但我如何使用返回的InStr值進行研究? – ufollettu