To delete specific color index row

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I tried to run the macro code below, but it does not even run when i
execute. Not sure if i have typed the code correctly??? Can someone advise?

Sub Testing()

ActiveSheet.Activate
Range("A1").Select


For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row

If Rows(i).Interior.ColorIndex = 6 Then
Rows(LastRow).Select
Selection.Delete

i = i + 1
End If

Next i

End Sub
 
Hi Junior,

You should try to avoid selections as these are usually
unnecessary and inefficient.Additionally, it is advisable
always to declare all variables explicitly.

Perhaps, however, you could explain what the variable
LastRow is intended to refer to and exactlty what the
procedure is intended to do
 
Hi

Not sure why your code isn't running and seeing as how your variable
isn't declared in your post its hard to know what you are trying to
do, though I presume from your title and the gist of the code you are
trying to delete all the instances of yellow rows within the used
range in column A? If so the code below should work for you.

As Norman so rightly said it isn't such a great idea to select things
if you don't have to and it is always a good idea to explicitly
declare your variables. Not only for your own use but when it comes
to posting a problem it is easier for people to read.

Add a new module then paste the code below into it.

Option Explicit
Dim MyCell, MyRng As Range
Dim LstCell As Integer

Sub DeleteYellowRows()

With ActiveSheet

LstCell = [A65536].End(xlUp).Row

Set MyRng = Range("A1:A" & LstCell)

For Each MyCell In MyRng

If MyCell.Interior.ColorIndex = 6 Then

MyCell.EntireRow.Delete

End If

Next MyCell

End With

End Sub

You can then call the macro from the macros menu Alt + F8 from the
main excel application window from here you can assign a keyboard
shortcut to the macro from the options button.

Hope this helps

S
 

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