как удалить файлы в папке, которые находятся в столбце B и красный - PullRequest
0 голосов
/ 26 сентября 2019

Мне нужна помощь.Мне нужно удалить старые файлы в папке.У меня есть список файлов в столбце B и старые файлы окрашены в RGB (255,0,0).Предполагая, что код будет выглядеть примерно так:

   Dim MyFolder As String
Dim MyFile As String
Dim cell As Variant
Dim source As Range
Set source = Range("c3:c8")
MyFolder = Sheets("Delete Revs").Range("K1").Value & "\"
MyFile = Dir(MyFolder & "\" & "*.*")
    For Each cell In source
        If cell.Interior.Color = RGB(255, 0, 0) Then
        Kill MyFile
        Else
        End If
Next

Ответы [ 2 ]

0 голосов
/ 27 сентября 2019
Sub DeleteFiles()

    Dim myFolder, myFile As String
    Dim Cel As Variant

    myFolder = Sheet1.[A1] & "\" 'Folder Path

  'Looping for Visible Cells Only
    For Each Cel In Sheet1.Range("C3:C8").SpecialCells(xlCellTypeVisible)

        myFile = myFolder & Cel 'File Path

        If Cel.Interior.Color = vbRed Then
            If Len(Dir$(myFile)) > 0 Then    'If File Exits in Folder
                Kill (myFile)  'Delete File
            End If
        End If

    Next Cel
End Sub
0 голосов
/ 27 сентября 2019

Используйте следующий код:

Sub DeleteFiles()
Dim MyFolder As String
Dim MyFile As String
Dim cell As Variant
Dim source As Range

MyFolder = Sheets("Delete Revs").Range("K1").Value & "\"
Set source = Range("c3:c8")

For Each cell In source
    If cell.Interior.Color = vbRed Then
        MyFile = MyFolder & cell.Value
        If Dir(MyFile) <> "" Then
            Kill MyFile
            cell.Interior.Color = vbGreen 'changing the color when file deleted
        End If
    End If
Next
End Sub
...