Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba &引用;entirerow.delete";跳过For循环中的条目_Vba_Excel_For Loop_Delete Row_Strikethrough - Fatal编程技术网

Vba &引用;entirerow.delete";跳过For循环中的条目

Vba &引用;entirerow.delete";跳过For循环中的条目,vba,excel,for-loop,delete-row,strikethrough,Vba,Excel,For Loop,Delete Row,Strikethrough,我试图清理一组数据,发现vba函数entirerow.delete有些奇怪 如果行的格式为删除线格式,下面的代码将按预期删除整行,但如果行的格式也是删除线格式,则将跳过紧随其后的行。这似乎是一个错误,它需要一个不是删除线格式的行来“重置”删除更多行的能力。有人知道为什么吗,或者我能做些什么来调试这个 For Each rng In rng1 'Check each character in the cell For i = 1 To Len(rng.Value) 'If any lett

我试图清理一组数据,发现vba函数entirerow.delete有些奇怪 如果行的格式为删除线格式,下面的代码将按预期删除整行,但如果行的格式也是删除线格式,则将跳过紧随其后的行。这似乎是一个错误,它需要一个不是删除线格式的行来“重置”删除更多行的能力。有人知道为什么吗,或者我能做些什么来调试这个

For Each rng In rng1
'Check each character in the cell
    For i = 1 To Len(rng.Value)
'If any letter is Strikethrough,delete entire column
        If rng.Characters(i, 1).Font.Strikethrough = True Then
            rng.Select    'just serves the purpose of observing which rows are being selected
            rng.EntireRow.Delete
        GoTo NextRng
        End If
    Next i
NextRng:
Next rng
我应该说,我已经找到了一个使用不同方法的解决方案,但速度非常慢:

'Delete cells that have the strikethrough format - works but is super slow!
ws2.Range("B2").Activate
Do Until ActiveCell.Value = ""
    If ActiveCell.Font.Strikethrough = True Then
        ActiveCell.EntireRow.Delete
        Else: ActiveCell.Offset(1, 0).Activate
    End If
Loop

如果有人能找到另一种快速解决此问题的方法,我也非常感谢您的意见。

找到您的范围:

Dim wSheet As Worksheet : Set wSheet = ThisWorkbook.Worksheets("Sheetname")
Dim lastRow
' gets the last row in col 1, adjust as required
lastRow = wSheet.Cells(wSheet.Rows.Count, 1).End(xlUp).Row 
现在向后执行For循环。您面临的问题是,删除一行时,数据会上移(例如:删除第56行,第57行变为第56行)。解决方法是从下往上删除

For myLoop = lastRow to 2 Step -1 ' or to 1 if you've no header row
    Set myRange  = wSheet.Range("A" & myLoop)
    For mySubLoop = 1 To Len(myRange.Value)
        'If any letter is strikethrough,delete entire row 
        If myRange.Characters(mySubLoop, 1).Font.Strikethrough = True Then
            myRange.EntireRow.Delete
            Exit For ' skip out of this inner loop and move to the next row
        End If
    Next
Next

多亏了你的快速反应,我才明白。特别感谢@Siddarth Rout在本帖中推动我采用(稍微)更快的方法: 以下是工作代码,以防有人好奇:

Dim delRange As Range
Dim ws2 As Worksheet
Dim i As Long

'Find Lastrow in ws2
LastRow2 = ws2.Cells.Find(What:="*", _
                After:=Range("A1"), _
                LookAt:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Row
With ws2
    For i = 1 To LastRow2
        If .Cells(i, 2).Font.Strikethrough = True Then
'This if statement adds all the identified rows to the range that will be deleted
            If delRange Is Nothing Then
                Set delRange = .Rows(i)
            Else
                Set delRange = Union(delRange, .Rows(i))
            End If
        End If
    Next i

    If Not delRange Is Nothing Then delRange.Delete
End With

如果要删除行,则向后循环。在这样的循环中,不应删除行。使用反向循环。搜索堆栈溢出。我已经回答了一篇帖子,请参见我的。您还可以使用范围对象来标识要删除的行。它比删除循环中的行快得多感谢关于反向循环的提示,但是现在它工作起来也非常慢@悉达思·罗特:我现在会查看你的帖子,看看我是否能理解你的观点method@ThomasInzina谢谢你的接球,非常感谢!++在达成解决方案方面做得很好:)