[英]Delete row if all formula-based cells in a certain range are 0 or blank
I am trying to write a code which basically looks at rows 13-33 and deletes the entire row if the cells in Columns BM are all Blank AND column A is NOT blank. 我正在尝试编写一个代码,该代码基本上着眼于第13-33行,并且如果BM列中的单元格全部为空白并且A列不是空白,则删除整行。 The issue that I am having is that all my cells are referencing the value from another sheet (formula-based).
我遇到的问题是我所有的单元格都在引用另一张表(基于公式)中的值。 When I run my code below, it does not seem to recognize these formula-based cells as "0"s even though that is there value.
当我在下面运行我的代码时,即使有值,似乎也无法将这些基于公式的单元格识别为“ 0”。
It only deletes the rows which have 0's but not referencing another cell. 它仅删除具有0的行,但不引用另一个单元格。 I do not want to have to go copy and paste everything as values before running this since I want to be able to keep the formulas.
我不想在运行此命令之前将所有内容复制并粘贴为值,因为我希望能够保留公式。
Please take a look below and advise on how I can do this. 请在下面查看并建议我该如何做。
Sub ScheduleB()
On Error GoTo errHandler
Const TOP_ROW As Long = 13
Const BOTTOM_ROW As Long = 33
Dim rowIndex As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With ThisWorkbook.Worksheets("Schedule A Template")
For rowIndex = .Cells(BOTTOM_ROW, "A").End(xlUp).Row To TOP_ROW Step -1
If Not IsEmpty(.Cells(rowIndex, "A").Value2) Then '...column A is not blank.
If Application.WorksheetFunction.CountA(.Range(.Cells(rowIndex, "B"), .Cells(rowIndex, "M"))) = 0 Then '...all cells on row rowIndex from columns B to M are blank.
.Rows(rowIndex).Delete Shift:=xlUp
End If
End If
Next
End With
Cleanup:
On Error Resume Next
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Exit Sub
errHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly, "Error"
Resume Cleanup
End Sub
Building on my answer to your preceding question , you can scan each row's B to M cells and decide whether you want to delete the row. 在我对上一个问题的回答的基础上,您可以扫描每行的B到M单元格,并确定是否要删除该行。
Sub ScheduleB()
On Error GoTo errHandler
Const TOP_ROW As Long = 13
Const BOTTOM_ROW As Long = 33
Dim rowIndex As Long
Dim cell As Excel.Range
Dim bDelete As Boolean
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With ThisWorkbook.Worksheets("Schedule A Template")
For rowIndex = .Cells(BOTTOM_ROW, "A").End(xlUp).Row To TOP_ROW Step -1
If Not IsEmpty(.Cells(rowIndex, "A").Value2) Then '...column A is not blank.
bDelete = True
For Each cell In .Range(.Cells(rowIndex, "B"), .Cells(rowIndex, "M")).Cells
If Not IsEmpty(cell.Value2) Then
If VarType(cell.Value2) = vbDouble Then
If cell.Value2 <> 0 Then
bDelete = False 'Not deleting because a numeric value is non-zero.
End If
Else
bDelete = False 'Not deleting because we've hit a non-blank, non-numeric value, such as a string or an error.
End If
End If
If Not bDelete Then
Exit For
End If
Next
If bDelete Then
'.Rows(rowIndex).Delete Shift:=xlUp
Else
Debug.Print "will not delete row " & CStr(rowIndex)
End If
End If
Next
End With
Cleanup:
On Error Resume Next
Set cell = Nothing
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Exit Sub
errHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly, "Error"
Resume Cleanup
End Sub
Your preceding question did not mention the presence of formulas. 您前面的问题没有提到公式的存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.