Вот решение, которое я придумала, и надеюсь, что это поможет кому-то еще!Пожалуйста, дайте мне знать, если у вас есть какие-либо предложения о том, как улучшить код.
Это отсканирует местоположение папки, предоставленной на листе 1 E3 Имена файлов будут добавлены в столбец 2 листа A Строка каждой ячейки в столбце A листа 1 столбца Aбудет сравниваться с колонкой листа 2 и помечен как хороший. Тогда все, что не отмечено как хорошее, помечается как плохое. Наконец, мы сканируем столбец А листа 2, ищем ячейки, помеченные как плохие, сравниваем имена с файлами и удаляем файл.
Private Sub CommandButton1_Click()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim j As Long
Dim l As Long
Dim lr1 As Long
Dim lr2 As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(ws1.Cells(3, 5).Value)
i = 1
'Scan through the folder and list files in Sheet2, column A
For Each objFile In objFolder.Files
ws2.Cells(i + 1, 1) = objFile.Name
i = i + 1
Next objFile
'Setup the sheets
lr1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
lr2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
For l = 2 To lr1
ws1.Cells(l, 1).Style = "normal"
Next l
For j = 2 To lr2
ws2.Cells(j, 1).Style = "normal"
Next j
'Check cell string in Sheet1 column A against file names
'in Sheet2 column A and flag both Good
For l = 2 To lr1
cell1 = ws1.Cells(l, 1).Value
For j = 2 To lr2
cell2 = ws2.Cells(j, 1).Value
If InStr(1, cell2, cell1) > 0 Then
ws1.Cells(l, 1).Style = "Good"
ws2.Cells(j, 1).Style = "Good"
End If
Next j
Next l
'Scan both Sheets 1 and 2 for unmarked cells and flag Bad
For l = 2 To lr1
style1 = ws1.Cells(l, 1).Style
If style1 = "Normal" Then
ws1.Cells(l, 1).Style = "Bad"
End If
Next l
For j = 2 To lr2
style2 = ws2.Cells(j, 1).Style
If style2 = "Normal" Then
ws2.Cells(j, 1).Style = "Bad"
End If
Next j
'Delete files if Sheet2 Column A cells are marked Bad and the
'cell string matches the file name
For j = 2 To lr2
cell2 = ws2.Cells(j, 1).Value
style2 = ws2.Cells(j, 1).Style
For Each objFile In objFolder.Files
If style2 = "Bad" And objFile.Name = cell2 > 0 Then
Kill objFile
End If
Next objFile
Next j
MsgBox "Complete"
End Sub