0

I have an excel spreadsheet of a BOM that I'm trying to conditionally format. The data is laid out such that column A is the item number. Since the BOM has alternates, there are repeated numbers. I want to go through the spreadsheet and for each item number, find the item with "Active" in column F and highlight them green and hide the alternates on the other rows. If there is no "active" item, I want to highlight the items as yellow and keep them displayed. I have the current vba script which does the highlighting. If you look at the example data I basically want a single line for each item number which shows the active part, but if there is no active part, to show the historical or discontinued parts in yellow

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim icolor As Integer
Dim lastrow As Long
Dim i As Integer
Dim cell As Range
Dim sheetname As String
sheetname = Application.ActiveSheet.Name
With Worksheets(sheetname)
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Application.ScreenUpdating = False


For Each cell In Range("F1:F" & lastrow)
    Select Case cell.Value
        Case Is = "Active"
            cell.EntireRow.Interior.ColorIndex = 10
        Case Is = "Status"
            cell.EntireRow.Interior.ColorIndex = 15
        Case Is = ""
            cell.EntireRow.Interior.ColorIndex = 2
            cell.EntireRow.Hidden = True
        Case Else
            cell.EntireRow.Interior.ColorIndex = 6
    End Select
Next cell
Application.ScreenUpdating = True
End Sub

Here's a screenshot of some sample data: Data Sample

5
  • Can you please provide an image of your data as well as your expected result? That may help others in understanding how everything works and what you're looking for. Commented Feb 14, 2018 at 23:35
  • I uploaded a screenshot but I can't embed the picture due to a my reputation being too low Commented Feb 14, 2018 at 23:55
  • I went ahead and edited it for you. Once my edit is approved you should see it embedded. Welcome to the site! Commented Feb 14, 2018 at 23:59
  • Why do you want to do that on every selection change? Shouldn't it be Worksheet_Change? Since the cells in col F doesn't change automatically. Commented Feb 15, 2018 at 1:43
  • The excel spreadsheet is an output of a database so I wanted to be able to copy the vba script to each spreadsheet so it would run once when I changed the selection. That way I make sure I don’t change a cell value to get it to run. Commented Feb 15, 2018 at 6:04

2 Answers 2

1

You need to set the row height to zero to hide it

cell.EntireRow.RowHeight = 0

But don't forget to reset it in the other two cases

 cell.EntireRow.AutoFit
Sign up to request clarification or add additional context in comments.

Comments

0

Please check to see if the cell value is NULL as well as ""

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.