Excel fornual to delect rows

  • Thread starter Thread starter sspande
  • Start date Start date
S

sspande

I am writing a Macro and wondering if there is away to delect a row if
cell b and cell c =0?
 
you don't share a whole lot about what you want to do. try one of these:

Option Explicit

Sub test()
Dim ws As Worksheet
Set ws = Worksheets("sheet1")

With ws
If Cells(ActiveCell.Row, "B") = 0 And Cells(ActiveCell.Row, "B") = 0 Then
MsgBox "both = 0"
End If
End With

End Sub
Sub test2()
Dim ws As Worksheet
Set ws = Worksheets("sheet1")
Dim lastrow As Long
Dim cell As Range
lastrow = ws.Cells(Rows.Count, "B").End(xlUp).Row

With ws
For Each cell In .Range("B1:B" & lastrow)
If Application.Sum(cell.Resize(1, 2)) = 0 Then
MsgBox "row " & cell.Row & " both are 0"
End If

Next
End With

End Sub
 
Just be aware that in your sub test2() below that if one cell is 6, and the
other -6 it will still say both are zero.

Also, if by 'delete' the original poster meant just to clear the contents,
with a line such as:
cell.EntireRow.ClearContents
to replace the msgbox line, then the macro will work as required.

If instead, the OP wanted to remove the row and close up the gap with
cell.EntireRow.Delete
then if there are two rows next to each other which need deleting, it will
miss the second one, because it runs from the top downwards.

So to be constructive, try it with these small adjustments

Sub test2()
Dim ws As Worksheet
Set ws = Worksheets("sheet1")
Dim lastrow As Long
Dim cell As Range
Dim i As Integer
lastrow = ws.Cells(Rows.Count, "B").End(xlUp).Row

With ws.Range("B1:B" & lastrow)
For i = .Cells.Count To 1 Step -1
If .Cells(i) = 0 And .Cells(i).Offset(0, 1) = 0 Then
MsgBox "row " & .Cells(i).Row & ", cells B and C are both 0"
.Cells(i).EntireRow.Delete
End If
Next i
End With

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top