如何删除包含数据的范围中的每一行?
在大数据集中,我试图findJ列中的每个单元格,其中包含任何数据,以便“不是空白”,然后删除包含数据的那一行。 我的代码,但不会为它工作:
Dim rng3 As Range Do Set rng3 = Columns("J:J") If Not rng3 Is Nothing Then rng.EntireRow.Delete Else Exit Do End If Loop
我希望有人能帮助我。
- select你需要的范围
- 按
F5
- 按
Special...
button - select
Constants
或Formulas
(如果需要,可以select其他选项) - 按
OK
- 现在进入:
Home
– >Cells
– >Delete
(箭头) – >Delete Sheet Rows
UPDATE
macros:
Sub DeleteRowsWithData() Dim rng As Range ' Change to the range you need Set rng = Range("A1").CurrentRegion ' Change field to the one you need rng.AutoFilter Field:=1, Criteria1:="<>", Operator:=xlFilterValues With rng With .Offset(1).Resize(.Rows.Count - 1) 'Exclude header Application.DisplayAlerts = False .Rows.Delete Application.DisplayAlerts = True End With End With End Sub
好几次:
你testingRange
不是Nothing
– 它总是会返回True
因为它实际上是指一个对象。 要testing单元格的内容,请使用IsEmpty()
。
如果删除行,则向后运行循环。
Sub Test() Dim Rng As Range Dim I As Integer 'You need only non-empty cells, thus the intersection of needed column and used range of your worksheet Set Rng = Intersect(ActiveSheet.UsedRange, Columns("J")) 'Loop the cells backwards, from the last row to the first For I = Rng.Rows.Count To 1 Step -1 'If the cell is not empty, the delete that row and continue upwards If Not IsEmpty(Rng.Cells(I)) Then Rng.Cells(I).EntireRow.Delete End If Next End Sub
这个简单的代码将做的伎俩:
For i = Cells(Rows.Count, 10).End(xlUp).Row To 1 Step -1 If Cells(i, 10).Value <> "" Then Cells(i, 10).EntireRow.Delete End If Next i