[英]VBA Excel Delete Row Based on Value in Column
Would like to delete rows from a report based on the data in column M. Report is of variable size row-wise but the same width in columns.想要根据 M 列中的数据从报告中删除行。报告的行大小可变,但列的宽度相同。 "Valid" in a cell means it gets deleted.
单元格中的“有效”意味着它被删除了。
Sub Create()
Dim Range1 As Range
Set Range1 = Range("M:M")
For Each cell In Range1
If ActiveCell.Value = "Valid" _
Then ActiveCell.EntireRow.Delete
Next cell
End Sub
It now about the ActiveCell
but cells in the column "M:M".它现在是关于
ActiveCell
而是列“M:M”中的单元格。 Also, you need to start form the bottom up (not obvious but true).此外,您需要从自下而上开始(不明显,但确实如此)。 So, assuming there are fewer rows than 10000, you need something like this:
所以,假设行数少于 10000,你需要这样的东西:
Sub Create()
Dim LastRow As Long
Dim i As Long
LastRow = Range("M10000").End(xlUp).Row
For i = LastRow To 1 Step -1
If Range("M" & i) = "Valid" Then
Range("M" & i).EntireRow.Delete
End If
Next
End Sub
I found a way using your For Each
:我找到了一种使用
For Each
:
Public Sub Create()
Dim Range1 As Range
Dim Cell
Dim LastRow As Long
Set Range1 = Range("M1")
' assume, there is some data in the first row of your sheet
LastRow = Range1.CurrentRegion.Rows.Count
' otherwise, find last cell in column M with a value, assume before row 10000
LastRow = Range("M10000").End(xlUp).Row
' select the cells to process
Set Range1 = Range(Range1, Range1.Offset(LastRow, 0))
' process the rows
For Each Cell In Range1
If Cell.Value = "Valid" Then
Debug.Print "' delete row from at address :: " & Cell.Address
Range(Cell.Address).EntireRow.Delete
End If
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.