简体   繁体   English

如何根据在Excel中单击的值突出显示多个单元格

[英]How to highlight multiple cells based on value on click in Excel

Context 语境

The Excel file is made up of 2 parts. Excel文件由两部分组成。

  1. A regular data set (Picture included below) 常规数据集(下面的图片包括在内)
  2. 2D-representation of multiple machine modules and their corresponding cable trenches. 多个机器模块及其相应电缆槽的2D表示。

The Task 任务

I'd like to highlight the corresponding cells in the 2D-representations when I click on an ID in the data set . 当我单击数据集中的ID时,我想突出显示 2D表示形式中的相应单元格

This mockup-image shows the desired effect. 此模型图像显示了所需的效果。 多个选定字段的图像,这些字段应包含重复项 As seen in the image, there are multiple cells with duplicate values in the 2D-represenatation that needs highlighting 如图所示,二维表示中有多个具有重复值的单元格需要突出显示

How would one go about doing something like this? 人们将如何做这样的事情?

You should use VBA as Jerry says. 您应该像Jerry所说的那样使用VBA。 I would look at the Worksheet_SelectionChange Event To trigger the highlighting and use a for-loop to go trough the columns to check if the corresponding cell needs to be highlighted. 我将查看Worksheet_SelectionChange事件以触​​发突出显示,并使用for循环遍历各列,以检查相应的单元格是否需要突出显示。

As Alex suggested, the Worksheet_SelectionChange Event was a the way to go. 正如Alex所建议的,Worksheet_SelectionChange事件是一种方法。 It is now working as intended. 现在它正在按预期工作。

按预期工作

Here's what I wrote. 这就是我写的

\\Constructor
\\ResetColors
\\MakeTrenchYellow subroutines here.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1 And 3 - Selection.Cells.Count > 1 Then
        Constructor
        ResetColors

        Dim SelectedRowTextjoin As String
        SelectedRowTextjoin = Target.Offset(0, 6).Value

        Dim CurrentResult As Variant
        CurrentResult = Split(SelectedRowTextjoin, ", ")

        Dim AmountOfElements As Integer
        For Each Item In CurrentResult
            AmountOfElements = AmountOfElements + 1
        Next

        For i = 1 To AmountOfElements
            MakeTrenchYellow (CurrentResult(i - 1))
        Next i
    End If
End Sub

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

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