[英]Excel, VBA, Conditional Formatting
我需要一些帮助。 我相信 VBA 是解决这个问题的唯一方法,但我可能错了。
在我的工作簿的 Sheet1 上,我有两列包含不同的项目。 例如,在 B 列和 F 列中包含不同的设备项目,它们旁边有一个空白的数量。 它用作检查清单。 在页面顶部的 Sheet1 上还有一些条件格式的复选框,这些复选框是为各种“框”选择的(例如:框 1、框 2、框 3 等)
在 Sheet2 上,如上所述,每个框都有不同的表命名,并且表中有不同的项目。 这些项目可能是也可能不是 Sheet1 上 col B & F 中的相同项目。
Purpose: I am hoping to learn to write code that says when selecting the conditional formatted check boxes next to the various boxes on Sheet 1, then it will highlight the items on sheet1 if it matches any item in the selected Box from Sheet2.
****我已经用下面的代码更新了我的问题。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' In order to run code on sheet without a button or enabling in a module
Set KeyCells = Range("A2")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is
Nothing Then
' Display a message when one of the designated cells has been changed.
Dim i, j As Integer
Dim box As String
Dim c As Range 'Define two ranges so that we can loop through both sheets to
check the boxes
Dim d As Range
Sheets(1).Range("B11:B30, F11:F30").Font.ColorIndex = 0 'Remove the cell styles to apply new ones
box = Sheets(1).Cells(2, 1) 'This refers to the checkbox - **QUESTION:How to have "multiple" check boxes to select from and will run the same code?**
For i = 1 To 10 'Loop to find the checked box in sheet2
If Sheets(2).Cells(1, i) = box Then 'Check for checked box
For Each c In Sheets(2).Range(Sheets(2).Cells(2, i), Sheets(2).Cells(6, i))
For Each d In Sheets(1).Range("B11:B30, F11:F30")
If c = d Then
Sheets(1).Cells(d.Row, d.Column).Font.ColorIndex = 3 'changes matching item to red font
End If
Next d
Next c
End If
Next i
End If
End Sub
根据我从您的问题中理解的内容,我编写了一个代码来格式化单元格颜色。 我已经给出了注释和代码。
Sub format()
Dim i As Integer
Dim box As String
Dim c As Range 'Define two ranges so that we can loop through both sheets to check the boxes
Dim d As Range
Sheets(1).Range(Cells(4, 1), Cells(50, 50)).Interior.ColorIndex = xlNone 'Remove the cell styles to apply new ones
box = Sheets(1).Cells(1, 1) 'This refers to the checkbox
For i = 1 To 10 'Loop to find the checked box in sheet2
If Sheets(2).Cells(1, i) = box Then 'Check for checked box
For Each c In Sheets(2).Range(Sheets(2).Cells(2, i), Sheets(2).Cells(20, i))
For Each d In Sheets(1).Range(Cells(4, 2), Cells(21, 21))
If c = d Then
Sheets(1).Cells(d.Row, d.Column).Interior.ColorIndex = 6 'If true give yellow colour
End If
Next d
Next c
End If
Next i
End Sub
请参阅下面的代码,我到目前为止。 这是我上面收到的变体。 我已将问题发布在第 13 行。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' In order to run code on sheet without a button or enabling in a module
Set KeyCells = Range("A2")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is
Nothing Then
' Display a message when one of the designated cells has been changed.
Dim i, j As Integer
Dim box As String
Dim c As Range 'Define two ranges so that we can loop through both sheets to check the boxes
Dim d As Range
Sheets(1).Range("B11:B30, F11:F30").Font.ColorIndex = 0 'Remove the cell styles to apply new ones
box = Sheets(1).Cells(2, 1) 'This refers to the checkbox - **QUESTION: How to have "multiple" check boxes to select from and will run the same code?**
For i = 1 To 10 'Loop to find the checked box in sheet2
If Sheets(2).Cells(1, i) = box Then 'Check for checked box
For Each c In Sheets(2).Range(Sheets(2).Cells(2, i), Sheets(2).Cells(6, i))
For Each d In Sheets(1).Range("B11:B30, F11:F30")
If c = d Then
Sheets(1).Cells(d.Row, d.Column).Font.ColorIndex = 3 'changes matching item to red font
End If
Next d
Next c
End If
Next i
End If
End Sub
表 1 复选框- 我有条件格式的复选框,我可以更改。 他们基于 0,1 的数据验证列表来更改填充。 但我可能不得不改变这一点。
我已经回答了我自己的问题。 基本上,您可以拥有多个字符串 box1、box2 等,并为每个字符串编写相同的代码。 不确定这是否是漫长的过程,但它确实有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.