我尝试删除Excel 2013中的整行,但前提是列K,L和M中的所有单元格都是0 / $ 0.00。
我的数据示例: Excel Data Sheet
我希望它保留第2 - 11行,因为它们都包含K,L或M中的内容。我发现并尝试使用的当前代码似乎只是识别列L和M,因为它正在删除在K列中有一个数字的第2行。我不希望它连续计算3个单元格的总数,因为如果我在K列中有500美元的数字而在L列中有$ 500,它们等于$ 0.00但我需要那一行,因为有数据。
我发现了两个与我在本网站上提出的问题非常相似的问题,所以我尝试将代码应用到我正在做的事情上,但我一定是做错了,因为我无法做到这一点。让它发挥作用。
Excel VBA delete entire row if both columns B and C are blank
Delete entire row if cells in specific range are all empty
这是我发现并尝试使用的代码。难道它不能正常工作,因为1列是正数而其他2是负数?我对使用VBA等非常陌生,所以如果这很简单,我很抱歉。
Sub DeleteRows()
Dim rng As Range, cel As Range
Dim N As Long
For N = rng.Rows.Count To 1 Step -1
If rng.Cells(N, 1) = 0 And rng.Cells(N, 2) = 0 Then
rng.Cells(N, 1).EntireRow.Delete shift:=xlShiftUp
End If
Set rng = ActiveSheet.Range("L1:L" & ActiveSheet.Range("L" & ActiveSheet.Rows.Count).End(xlUp).Row)
If rng.Cells(N, 1) = 0 And rng.Cells(N, 2) = 0 Then
rng.Cells(N, 1).EntireRow.Delete shift:=xlShiftUp
End If
Set rng = ActiveSheet.Range("M1:M" & ActiveSheet.Range("M" & ActiveSheet.Rows.Count).End(xlUp).Row)
If rng.Cells(N, 1) = 0 And rng.Cells(N, 2) = 0 Then
rng.Cells(N, 1).EntireRow.Delete shift:=xlShiftUp
End If
Next N
End Sub
我实际使用并每天使用的电子表格通常包含12,000到15,000行(文件大小总是大约2.5MB)。 我真的很感激能帮助我做些什么来完成这项工作。 谢谢
答案 0 :(得分:1)
如果我理解正确的话:
Sub DeleteRows()
Dim rw As Range, r
'start on the last row
With ActiveSheet.Range("A1").CurrentRegion.EntireRow
Set rw = .Rows(.Rows.Count)
End With
Do While rw.Row > 11
r = Application.CountIf(rw.Cells(1, "K").Resize(1, 3), 0)
Set rw = rw.Offset(-1, 0)
If r = 3 Then rw.Offset(1, 0).Delete
Loop
End Sub
答案 1 :(得分:0)
您可能想尝试以下代码:
Option Explicit
Sub DeleteRows()
With ActiveSheet '<--| refer to active sheet (you may want to explicitly refer to a named worksheet: 'With Worksheets("mySheet")')
With .Range("A1").CurrentRegion.Offset(, .UsedRange.Columns.Count).Resize(, 1) '<--| refer to a range in a "helper" column just outside the used range occupying the same rows as your data
.FormulaR1C1 = "=if(countif(RC11:RC13,0)=3,1,"""")" '<--| use "helper" column to mark "KLM-zero's" rows with a "1", while leaving others with a "blank" mark
If WorksheetFunction.Sum(.Cells) > 0 Then .SpecialCells(XlCellType.xlCellTypeFormulas, xlNumbers).EntireRow.Delete '<--| delete any row whose "helper" column cell is marked with "1"
.Clear '<--| clear "helper" column
End With
End With
End Sub