【问题标题】:For loop to delete rowsFor循环删除行
【发布时间】:2016-04-25 16:16:05
【问题描述】:

我正在使用一个宏,它列出了我选择的任何目录中的所有文件名。我正在编写代码,将文件名分解成以后可以使用的块。文件名列表从单元格 F6 开始并沿列向下延伸。这是我到目前为止编写的代码:

Dim ContractNum As String
Dim InvNum As String
Dim FileRng As Range
Dim FileLastRow As Long
Dim File As Range

FileLastRow = Sheet1.Range("F" & Rows.Count).End(xlUp).Row

Set FileRng = Sheet1.Range("F6:F" & FileLastRow).SpecialCells(xlCellTypeConstants, 23)

For Each File In FileRng
If File = "Invoice.zip" Or File = "Thumbs.db" Then
    File.EntireRow.Delete
End If
Next File


For Each File In FileRng
    ContractNum = Left(File, 4)
    InvNum = Mid(File, 8, 6)
    File.Offset(0, -5) = ContractNum
    File.Offset(0, -4) = InvNum
Next File

到目前为止,我的那部分工作正常。我遇到的问题是,在所有使用此宏的目录中,都有不需要的文件,例如“Thumbs.db”或“Invoice.zip”。我遇到问题的代码如下:

For Each File In FileRng
If File = "Invoice.zip" Or File = "Thumbs.db" Then
    File.EntireRow.Delete
End If
Next File

我想要做的是扫描整个文件名列表,如果遇到“Thumbs.db”或“Invoice.zip”的文件名,删除整行。到目前为止,这工作......有点。例如,如果我的列表中有两个名为“Thumbs.db”和“Invoice.zip”的文件,我必须运行宏两次才能删除这两个文件。显然,我想一口气将它们全部消灭。

【问题讨论】:

  • 向后运行常规 for 循环 for I = filelastrow to 6 step -1

标签: vba excel for-loop


【解决方案1】:

好问题! @Scott Craner 的回答很好地解决了这个问题(顺便提一下),你最终得到了一个可读、有效的 VBA 片段。好东西!

我认为还有另一种快速删除行的方法:Range.Autofilter 策略!看看下面的以 cmets 开头的 Autofilter 策略:

Public Sub DeleteRowsWithAutofilter()

    Dim ContractNum As String
    Dim InvNum As String
    Dim FileRng As Range
    Dim FileLastRow As Long
    Dim File As Range
    Dim t As Single
    t = Timer

    FileLastRow = Sheet2.Range("F" & Rows.Count).End(xlUp).Row

    'Identify the total range of filenames, including the header
    Set FileRng = Sheet2.Range("F5:F" & FileLastRow)

    'Use the .Autofilter method to crush those
    'annoying 'Thumbs.db' or 'Invoice.zip' rows
    Application.DisplayAlerts = False
    With FileRng
        .AutoFilter Field:=1, Criteria1:="Thumbs.db", _
                              Operator:=xlOr, _
                              Criteria2:="Invoice.zip"
        .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Rows.Delete
    End With
    Application.DisplayAlerts = True

    'Turn off the autofilter safely
    With Sheet2
        .AutoFilterMode = False
        If .FilterMode = True Then
            .ShowAllData
        End If
    End With

    MsgBox "Damn son! 'Autofilter' strategy completed in " & Timer - t & " seconds."

End Sub

我在此处录制了演示这两种技术(For 循环和Range.Autofilter)的简短截屏视频:

https://www.youtube.com/watch?v=1U7Ay5voVOE

希望对您继续开发脚本有所帮助!

【讨论】:

  • 非常感谢!!我会在未来的项目中记住这一点。我在消息框部分笑得很厉害,哈哈。
  • 我很喜欢这个简短的截屏视频。绝对值得一看。
【解决方案2】:

根据我的 cmets,将 for 循环更改为:

For i = filelastrow to 6 step -1
   If Sheet1.Cells(i,6) = "Invoice.zip" Or Sheet1.Cells(i,6)  = "Thumbs.db" Then
        Sheet1.row(i).Delete
   End If
Next File

问题是,当一行被删除时,下面的行将成为该行,然后循环在移动到下一行时跳过它。然后它还会在最后的空行中移动。

通过倒退,这​​个问题就解决了。

【讨论】:

  • 非常感谢斯科特!经过一些修补后,这非常有效!
猜你喜欢
  • 1970-01-01
  • 2022-10-20
  • 2013-06-26
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 2018-09-03
  • 1970-01-01
相关资源
最近更新 更多