+ Reply to Thread
Results 1 to 13 of 13

Thread: How do I delete the row when it has one cell highlighted?

  1. #1
    Registered User
    Join Date
    12-03-2011
    Location
    The end of the rainbow
    MS-Off Ver
    Excel 2003
    Posts
    12

    How do I delete the row when it has one cell highlighted?

    Hi,
    My data has about 5000 rows and 30 columns.

    Based on a criteria, I have used conditional formatting in excel to highlight some cells. I would like to delete all the rows that have a cell highlighted (please refer to attached excel file for example). How may I do this?

    Thank you.


    Regards,
    Ashley
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: How do I delete the row when it has one cell highlighted?

    Hi,
    Which version of Excel are you actually using? Your profile says 2003 but that workbook is in the newer format and, if you have 2010, it is reasonably easy to do.
    Good luck.

  3. #3
    Registered User
    Join Date
    12-03-2011
    Location
    The end of the rainbow
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: How do I delete the row when it has one cell highlighted?

    Hi,
    I'm using my friend's computer and he has excel 2010. My excel is 2003. You can guide me on how to do this using excel 2010 as I am using his com now.

    Thank you.


    Regards,
    Ashley

  4. #4
    Registered User
    Join Date
    04-19-2010
    Location
    Ohio, USA
    MS-Off Ver
    Excel 2007
    Posts
    76

    Re: How do I delete the row when it has one cell highlighted?

    Ashley,

    You could make another formula row that gives you a true false row that would make it easier to check if the row needs to be deleted.

    Or you could check every cell in the range.
    Sub deleteRow()
    Dim myRange As Range
    Dim myCell As Range
    
    Set myRange = Range("A3:D8")
    
    
    For Each myCell In myRange
    If myCell.Value = "hh" Or myCell.Value = "pp" Or myCell.Value = "ss" Then
     myCell.EntireRow.Delete
    End If
    
    
    Next myCell
    End Sub
    I was having trouble with extracting the conditional state from the cell object too, I just replicated the conditions in the if block for the code.

    Either way Range().entireRow.delete command will do what you want.

  5. #5
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: How do I delete the row when it has one cell highlighted?

    For 2010, you can use the DisplayFormat like this
    Sub delHighlightedCells()
       Dim rgToDelete As Range
       Dim i As Long, j As Long
       
       For i = ActiveSheet.UsedRange.Row To ActiveSheet.UsedRange.Rows.Count
          For j = ActiveSheet.UsedRange.Column To ActiveSheet.UsedRange.Columns.Count
             If Cells(i, j).DisplayFormat.Interior.Color = 255 Then
                If rgToDelete Is Nothing Then
                   Set rgToDelete = Rows(i)
                Else
                   Set rgToDelete = Union(rgToDelete, Rows(i))
                End If
                Exit For
             End If
          Next j
       Next i
       If Not rgToDelete Is Nothing Then rgToDelete.Delete
    End Sub
    Good luck.

  6. #6
    Registered User
    Join Date
    12-03-2011
    Location
    The end of the rainbow
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: How do I delete the row when it has one cell highlighted?

    Hi all,
    I should have mentioned this earlier, I'm a VBA noob.

    Bmoe,
    Am I right to say that your code doesn't check for highlights, it just detects a certain value in cells and deletes a row that has a cell with that value?

    OnErrorGoto0,
    I ran your code verbatim and it works perfectly.

    Thanks to both of you!!


    Regards,
    Ashley
    Last edited by Ashleyyy; 01-09-2012 at 07:13 AM. Reason: 255 is white

  7. #7
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: How do I delete the row when it has one cell highlighted?

    255 is the RGB value for Red.
    Good luck.

  8. #8
    Registered User
    Join Date
    12-03-2011
    Location
    The end of the rainbow
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: How do I delete the row when it has one cell highlighted?

    Hi OnErrorGoto0,
    Thank you!

    Regards,
    Ashley

  9. #9
    Registered User
    Join Date
    12-03-2011
    Location
    The end of the rainbow
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: How do I delete the row when it has one cell highlighted?

    Hi,
    I just tried to run this code again:
    Sub delHighlightedCells()
       Dim rgToDelete As Range
       Dim i As Long, j As Long
       
       For i = ActiveSheet.UsedRange.Row To ActiveSheet.UsedRange.Rows.Count
          For j = ActiveSheet.UsedRange.Column To ActiveSheet.UsedRange.Columns.Count
             If Cells(i, j).DisplayFormat.Interior.Color = 255 Then
                If rgToDelete Is Nothing Then
                   Set rgToDelete = Rows(i)
                Else
                   Set rgToDelete = Union(rgToDelete, Rows(i))
                End If
                Exit For
             End If
          Next j
       Next i
       If Not rgToDelete Is Nothing Then rgToDelete.Delete
    End Sub
    and excel throws up the error

    Runtime error 438, object does not support this property of method.

    When I hit debug, excel highlights

    If Cells(i, j).DisplayFormat.Interior.Color = 255 Then


    Help?

    Thanks
    Last edited by Leith Ross; 02-11-2012 at 03:43 PM. Reason: Added Code Tags

  10. #10
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: How do I delete the row when it has one cell highlighted?

    Are you running it in 2010?
    Good luck.

  11. #11
    Registered User
    Join Date
    12-03-2011
    Location
    The end of the rainbow
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: How do I delete the row when it has one cell highlighted?

    yes, i am.

  12. #12
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: How do I delete the row when it has one cell highlighted?

    Then it should work. (it won't work in any other version) What format is the file saved in?
    Good luck.

  13. #13
    Forum Contributor
    Join Date
    10-06-2008
    Location
    Canada
    Posts
    141

    Re: How do I delete the row when it has one cell highlighted?

    You could try to see what the actual color is by selecting the colored cell and running this

    Sub ShowColour()
    Dim RGBColour As String, R As Integer, G As Integer, B As Integer
    RGBColour = Right("000000" & Hex(ActiveCell.Interior.Color), 6)
    R = WorksheetFunction.Hex2Dec(Right(RGBColour, 2))
    G = WorksheetFunction.Hex2Dec(Mid(RGBColour, 3, 2))
    B = WorksheetFunction.Hex2Dec(Left(RGBColour, 2))
    MsgBox "RGB" & vbTab & R & ", " & G & ", " & B & vbCrLf & "Index" & vbTab & ActiveCell.Interior.ColorIndex
    End Sub
    If you use a color from the color palette you can change the

    If Cells(i, j).DisplayFormat.Interior.Color = 255 Then
    to
    If Cells(i, j).DisplayFormat.Interior.ColorIndex = ?? Then  '<---- substitude question marks with number from Message Box
    Might work

    Regards
    John

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0