Delelting previous row

  • Thread starter Thread starter Novice Lee
  • Start date Start date
N

Novice Lee

What I am trying to do is, check if Row 5, Column D has "SB" in the cell. If
there is a "SB" in the cell, delete the row above it. I have done a search on
deleting row most of the posting were for deleting blank lines and the such
so they weren't much help to me. I am still trying to learn this stuff as
fast as I can.

Thanks
 
hi,,

Giving an answer that you can develop into more meaaningful code is
difficult given that you would normally do something as simple as this
without resorting to code i.e. you would simply look at d5 and delete
manually if required. However, this does what you want


Sub Stance()
If Cells(5, 4).Value = "SB" Then
Rows(4).EntireRow.Delete
End If
End Sub

Mike
 
What I am trying to do is, check if Row 5, Column D has "SB" in the cell.If
there is a "SB" in the cell, delete the row above it. I have done a search on
deleting row most of the posting were for deleting blank lines and the such
so they weren't much help to me. I am still trying to learn this stuff as
fast as I can.

Thanks

Hello, Try this:

Sub Test)
If Cells(5, "D").Value = "SB" Then Cells(5, "D").Offset(-1,
0).EntireRow.Delete
End Sub
 
What I am trying to do is, check if Row 5, Column D has "SB" in the cell.If
there is a "SB" in the cell, delete the row above it. I have done a search on
deleting row most of the posting were for deleting blank lines and the such
so they weren't much help to me. I am still trying to learn this stuff as
fast as I can.

Thanks

Sub DeleteSB()
Dim MyCell As Range
Dim SBRow As Range

Set SBRow = Range("5:5")

For Each MyCell In SBRow
If MyCell.Value = "SB" Then
MyCell.Offset(-1, 0).EntireRow.Delete
End If
Next
End Sub

This will delete row 4 if Row 5 contains "SB". If you want to delete
the previous row of any range that contains "SB", simply change the
line

SetSBRow = ActiveSheet.UsedRange.Rows
 
I guess I forgot to mention that it would check each cell in column D for a
"SB" throughout the entire sheet that sheet.
 
I guess I forgot to mention that it would check each cell in column D fora
"SB" throughout the entire sheet that sheet.

Revision to my code

Sub DeleteSB()
Dim MyCell As Range
Dim SBRow As Range

Set SBRow = Range("D:D")

For Each MyCell In SBRow
If MyCell.Value = "SB" Then
MyCell.Offset(-1, 0).EntireRow.Delete
End If
Next
End Sub

This will do what you want.
 
sub trythis()
for i=cells(rows.count,"d").end(xlup).row to 2 step-1
if ucase(cells(i,"d"))="SB" the rows(i-1).delete
next i
end sub
 
Yes you did neglect to mention that. have a look at your other response
 
Thanks it work perfectly

Revision to my code

Sub DeleteSB()
Dim MyCell As Range
Dim SBRow As Range

Set SBRow = Range("D:D")

For Each MyCell In SBRow
If MyCell.Value = "SB" Then
MyCell.Offset(-1, 0).EntireRow.Delete
End If
Next
End Sub

This will do what you want.
 

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