[英]Delete entire row if one or more cells are blank in any column
I am trying to iterate through rows and delete a row if there is a blank cell in the row, on a button click, regardless of what column it is in.我正在尝试遍历行并删除一行,如果该行中有一个空白单元格,则单击按钮,无论它位于哪一列。
I have 151 rows and Columns A to L.我有 151 行和 A 到 L 列。
When I run the code all my rows disappear.当我运行代码时,我的所有行都消失了。
Sub Button3_Click()
Dim lRow As Long
Dim iCntr As Long
Dim col As Integer
col = 1
lRow = 151
For iCntr = lRow To 1 Step -1
If Trim(Cells(iCntr, col)) = "" Then
Rows(iCntr).Delete
End If
col = col + 1
Next
End Sub
You need to loop across a fixed set of columns too - at the moment you appear to be just examining just one one cell per row - the col = col + 1
means you examine a diagonal stripe of cells, different column on each row.您还需要遍历一组固定的列 - 目前您似乎只检查每行一个单元格 - col = col + 1
意味着您检查单元格的对角条纹,每行上的不同列。
Anyway it's better to collect all deletion rows and delete at the end of the process, so that you're not altering the space you're examining.无论如何,最好收集所有删除行并在过程结束时删除,这样您就不会改变您正在检查的空间。
Sub Button3_Click()
Dim irx As Long
Dim ARow As Range
Dim ACell As Range
Dim DelSet As Range
Set DelSet = Nothing
For irx = 1 To 151
Set ARow = Range(Cells(irx, 1), Cells(irx, 12))
For Each ACell In ARow
If Trim(ACell.Value) = "" Then
If DelSet Is Nothing Then
Set DelSet = ACell.EntireRow
Else
Set DelSet = Union(DelSet, ACell.EntireRow)
End If
Exit For
End If
Next ACell
Next irx
If Not DelSet Is Nothing Then
DelSet.Delete
End If
End Sub
To see what this is doing, without actually deleting anything, you could change the DelSet.Delete
into DelSet.Select
and it will show you what the allocated region for deletion is.要查看这是在做什么,而不实际删除任何内容,您可以将DelSet.Delete
更改为DelSet.Select
,它将显示分配的删除区域是什么。
I recognize from your comment that this use of Range
object variables might be new to you, but definitely worth learning.我从您的评论中认识到,使用Range
object 变量对您来说可能是新的,但绝对值得学习。
rg
) is blank ( empty
, =""
, '
etc.), then the row range ( rrg
) will be combined (using Union
) into the Delete Range ( drg
).如果“给定”范围 ( rg
) 行中的任何单元格为空白 ( empty
、 =""
、 '
等),则行范围 ( rrg
) 将 (使用Union
) 组合到删除范围 ( drg
)。 Finally, the entire rows of the Delete Range will be deleted.最后,删除范围的整行都将被删除。Trim
may indicate that you also want to delete rows containing spaces.您使用Trim
可能表明您还想删除包含空格的行。 This is not covered by this solution (a cell is not blank if it contains a space).此解决方案未涵盖这一点(如果单元格包含空格,则单元格不是空白)。A Quick Fix快速修复
Option Explicit
Sub Button3_Click()
Const Cols As String = "A:L"
Const First As Long = 1
Const Last As Long = 151
Dim rg As Range: Set rg = Columns(Cols).Rows(First & ":" & Last)
Dim drg As Range
Dim rrg As Range
For Each rrg In rg.Rows
If Application.CountBlank(rrg) > 0 Then
If drg Is Nothing Then
Set drg = rrg
Else
Set drg = Union(drg, rrg)
End If
End If
Next rrg
If Not drg Is Nothing Then
drg.EntireRow.Delete
End If
End Sub
Your Idea Fixed你的想法固定
Sub Button3_Click()
Const fRow As Long = 1
Const lRow As Long = 151
Const fCol As Long = 1 ' ("A")
Const lCol As Long = 12 ' ("L")
Dim r As Long, c As Long
For r = lRow To fRow Step -1
For c = fCol To lCol
If Trim(Cells(r, c).Value) = "" Then
Rows(r).Delete
Exit For ' found and deleted: don't loop the columns any more
End If
Next c
Next r
End Sub
you need to select the row using the sintaxis: Eg Rows(nrow:nrow).select --> Rows(4:4).select你需要 select 使用 sintaxis 的行:例如 Rows(nrow:nrow).select --> Rows(4:4).Z99938282F04071859941E18F16EF
But you are counting in countdown, I guess you will need to count how many rows you have deleted.但是你正在倒计时,我想你需要计算你删除了多少行。 to select the right row.到 select 右行。
Good luck祝你好运
Sub Button3_Click()
Dim lRow As Long
Dim iCntr As Long
Dim col As Integer
col = 1
lRow = 151
For iCntr = lRow To 1 Step -1
For col = 1 To 12 '12 is L
If Trim(Cells(iCntr, col)) = "" Then
Rows(iCntr & ":" & iCntr).Select
Selection.Delete Shift:=xlUp
Exit For ' if found 1 blank, get out afeter delete
'After delet check wich cell you need to be stan on
End If
Next col
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.