简体   繁体   English

根据单元格值和周围的行进行格式化

[英]Formatting based on cell value and surrounding rows

I have an excel spreadsheet of a BOM that I'm trying to conditionally format. 我有一个要尝试有条件格式化的BOM表的Excel电子表格。 The data is laid out such that column A is the item number. 布置数据,使A列为项目编号。 Since the BOM has alternates, there are repeated numbers. 由于BOM有备用,因此有重复的数字。 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. 我想浏览电子表格,并为每个项目编号在F列中找到具有“有效”的项目,并将其突出显示为绿色,并在其他行上隐藏替代项。 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. 我有当前的vba脚本,高亮显示。 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: 这是一些示例数据的屏幕截图: 数据样本

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

请检查单元格值是否为NULL以及“”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM