【发布时间】:2020-11-05 03:45:42
【问题描述】:
目前我有一个宏,它可以查找随机放入文件夹/子文件夹中的文件,如果通过阅读 B 列中的列表找到它们,则打开它们。它工作得很好,但如果该文件没有,它只会跳过单元格t 存在,如果找不到,我想更改单元格的颜色。代码的上半部分来自其他人,我尝试过修改它,但找不到发生这种情况的方法。它不会添加不存在的文件路径,而是跳过它。
Sub GetFiles(StartFolder As String, Pattern As String, _
DoSubfolders As Boolean, ByRef colFiles As Collection)
Dim f As String, sf As String, subF As New Collection, s
If Right(StartFolder, 1) <> "\" Then StartFolder = StartFolder & "\"
f = Dir(StartFolder & Pattern)
Do While Len(f) > 0
colFiles.Add StartFolder & f
f = Dir()
Loop
sf = Dir(StartFolder, vbDirectory)
Do While Len(sf) > 0
If sf <> "." And sf <> ".." Then
If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
subF.Add StartFolder & sf
End If
End If
sf = Dir()
Loop
For Each s In subF
GetFiles CStr(s), Pattern, True, colFiles
Next s
End Sub
.
Sub BatchPrint()
Dim colFiles As New Collection
Dim CustRow, LastRow As Long
Set colFiles = New Collection
LastRow = Sheet1.Range("B9999").End(xlUp).Row
With Sheet1
For CustRow = 3 To LastRow
GetFiles "C:\Users\Desktop\Test\", Sheet1.Range("B" & CustRow) & ".pdf", True, colFiles
Next CustRow
End With
Dim i As Long
For i = 1 To colFiles.Count
FollowHyperlink colFiles(i)
Next i
Set colFiles = Nothing
End Sub
【问题讨论】: