Excel VBA搜索字符串和子字符串
我必须从Excel列表开始搜索和复制文件夹中的多个文件,如:Excel VBA搜索字符串和子字符串,excel,string,vba,file,search,Excel,String,Vba,File,Search,我必须从Excel列表开始搜索和复制文件夹中的多个文件,如: 8100 8152 8153 在该文件夹中,有以下文件名: 8100.pdf 100_8152.pdf 102_8153.pdf 8153 (2).pdf 如何在不重命名所有文件的情况下搜索这些文件? 感谢用户3598756,这是我现在用于在excel列表和文件夹中搜索同名文件的代码: Option Explicit Sub cerca() Dim T As Variant Dim D As Variant T = VBA.F
8100
8152
8153
在该文件夹中,有以下文件名:
8100.pdf
100_8152.pdf
102_8153.pdf
8153 (2).pdf
如何在不重命名所有文件的情况下搜索这些文件?
感谢用户3598756,这是我现在用于在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
选项显式
cerca小组()
作为变体的dimt
作为变体的dimd
T=VBA.Format(VBA.Time,“hh.mm.ss”)
D=VBA.Format(VBA.Date,“yyyy.MM.dd”)
将源设置为字符串
将Dest变暗为字符串
朦胧如弦
找到作为字符串的Dim文件
作为变体的Dim codices
暗淡单元格作为范围
Source=“D:\myfolder\”
Dest=“D:\myfolder\research”&D&&T
如果Dir(Dest,vbDirectory)=“”,那么MkDir Dest'您应该像其他帖子一样:
1) 循环浏览目录中的所有文件
2) 测试文件名是否包含ContainsAny(字符串源,字符串[]str____查找,布尔区分大小写)
函数的任何字符串,该函数由上面链接的帖子中的“Mat's Mug”提出
3) 如果文件包含您正在搜索的任何字符串(函数返回TRUE),请复制该文件
Public Function ContainsAny(ByVal string_source As String, ByVal caseSensitive As Boolean, ParamArray find_strings() As Variant) As Boolean
Dim find As String, i As Integer, found As Boolean
For i = LBound(find_strings) To UBound(find_strings)
find = CStr(find_strings(i))
found = Contains(string_source, find, caseSensitive)
If found Then Exit For
Next
ContainsAny = found
End Function
除了函数之外,还可以使用带星号(*
)的Dir()
,如以下(注释)代码:
选项显式
子搜索()
将源变暗为字符串、目标变暗为字符串、丢失为字符串、文件找到为字符串
暗淡单元格作为范围
Source=“D:\varie\Lavoro\Programming\VBA\Forum\Stack Overflow\Test\”
Dest=“D:\varie\Lavoro\Programming\VBA\Forum\Stack Overflow\Test\output”
'Source=“D:\myfolder\”
'Dest=“D:\myfolder\research”
如果Dir(Dest,vbDirectory)=“”,那么MkDir Dest'查看函数InStr
-它将为您提供回答此问题所需的所有信息。如果您遇到问题,请回来解释问题,我们可以帮助您解决问题…谢谢Dave,但我如何使用返回的InStr值进行研究?非常感谢,经过一些编辑,代码可以工作,但我需要搜索多个源(“源”中的子文件夹),如source\2015、source\2016、,更新:宏只查找带有前缀的文件,而不查找没有前缀的文件,如“8152(2).pdf”
Option Explicit
Sub search()
Dim Source As String, Dest As String, Missed As String, fileFound As String
Dim cell As Range
Source = "D:\varie\Lavoro\Programming\VBA\Forum\Stack Overflow\Test\"
Dest = "D:\varie\Lavoro\Programming\VBA\Forum\Stack Overflow\Test\output"
'Source = "D:\myfolder\"
'Dest = "D:\myfolder\research"
If Dir(Dest, vbDirectory) = "" Then MkDir Dest '<--| cerate destination folder if not alerady there
With Worksheets("PDF") '<-- reference your worksheet with pdf names (change "PDF" to your actual sheet name)
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
fileFound = Dir(Source & "\*" & cell.Value & "*.Pdf") '<-- look for a source folder file whose name contains the current cell value
If fileFound <> "" Then '<-- if found...
FileCopy Source & 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, Left(Missed, Len(Missed) - 2)
Close #FF
End If
MsgBox "OK"
Shell "explorer.exe " + Dest, vbNormalFocus
End Sub