首页 > 解决方案 > 如何删除文件夹中B列和红色的文件

问题描述

我需要帮助。我需要删除文件夹中的旧文件。我在 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

标签: excelvbafor-loopdelete-file

解决方案


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

推荐阅读