针对当前活动工作表操作 单元格的值 删除指定列中符合条件值的整行。通过弹窗输入列字母和条件值,会自动识别该列中的内容并删除与条件匹配的行。如果条件值为空,则删除该列中空白单元格所在的整行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 Sub DeleteEmptyRows() Dim ws As Worksheet Dim inputStr As String Dim column As String Dim conditionValue As String Dim stopRow As Long Dim lastRow As Long Dim i As Long Dim deleteCount As Long Set ws = ThisWorkbook.ActiveSheet inputStr = InputBox ("请输入列字母、条件值和停止行号,以逗号分隔(停止行号默认为1):" , "选择列和条件值" ) If InStr (inputStr, "," ) = 0 Then MsgBox "输入格式无效,请使用 '列字母,条件值,停止行号' 格式。" , vbExclamation Exit Sub End If column = Trim (Split (inputStr, "," )(0 )) conditionValue = Trim (Split (inputStr, "," )(1 )) On Error Resume Next stopRow = CLng (Trim (Split (inputStr, "," )(2 ))) If stopRow <= 0 Then stopRow = 1 On Error GoTo 0 lastRow = ws.Cells(ws.Rows.Count, column).End (xlUp).Row deleteCount = 0 For i = lastRow To stopRow + 1 Step -1 If conditionValue = "" Then If IsEmpty (ws.Cells(i, column)) Or ws.Cells(i, column).Value = "" Then ws.Rows(i).Delete deleteCount = deleteCount + 1 End If Else If CStr (ws.Cells(i, column).Value) = conditionValue Then ws.Rows(i).Delete deleteCount = deleteCount + 1 End If End If Next i If deleteCount = 0 Then MsgBox "没有删除任何行,请检查条件是否输入正确!" , vbExclamation Else MsgBox "删除完成,共删除了 " & deleteCount & " 行。" , vbInformation End If End Sub
示例
输入 B,,5
: 删除 B
列中为空的行,但不会删除第 5 行及其以上的行。
输入 C,123,3
: 删除 C
列中值为 123
的行,但不会删除第 3 行及其以上的行。
输入 A,,1
: 删除 A
列中为空的行,但不会删除第 1 行。