【问题标题】:how to delete files in a folder that are in column B and red如何删除文件夹中 B 列和红色的文件
【发布时间】:2019-09-26 20:57:25
【问题描述】:

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

【问题讨论】:

  • 到底是什么问题?
  • If Cell.Interior.Color = vbRed Then: Kill Cell.Value
  • 我不知道如何编写代码。我还是新手,我找不到我想做的例子。
  • 我刚试过这段代码。 “修改了原文”
  • Read This。有大量免费的 VBA 教程可帮助您开始正确编写代码。

标签: excel vba for-loop delete-file


【解决方案1】:

使用以下代码:

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

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多