![](/img/trans.png)
[英]Excel VBA for hiding cells in one sheet if they match cells in another sheet
[英]Excel VBA, How to use the selection in a dropdown list on one sheet format(color) a range of cells on another sheet
从我收集到的信息来看,条件格式化无法在工作簿的各个工作表之间完成,因此我现在要尝试的是使用VBA。
我想在sheet1的下拉列表中选择一个词(比如说“是”或“否”)! 然后为一系列单元着色; 在sheet3!上的范围(“ J3:P29”)。 如果选择“是”,则范围将被着色;如果选择“否”,则范围将不被着色。
我使用了macrorecorder来记录下面的代码。 但是,它不会记录下拉列表中的选择。 它只是用逗号代替了动作。
Sub RangeRed()
'
' RangeRed Macro
'
'
Range("J3:P29").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
我将不胜感激一些帮助。 谢谢!
您需要进行几次更改。
Worksheet_Change
事件来更新sheet3。 Target
,评估它是否满足指定条件,并根据需要更新sheet3 打开VB编辑器,然后打开Sheet1代码。 然后粘贴以下代码。
Private Sub Worksheet_Change(ByVal Target As Range)
' Target.Value will be contain the value of the changed cell
If Target.Value = "Yes" Then
With ThisWorkbook.Sheets("Sheet3").Range("J3:J5").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Else
With ThisWorkbook.Sheets("Sheet3").Range("J3:J5").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 0 ' change to whatever color you want if Yes is not selected
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
End Sub
请注意, Target
将捕获工作表上更改的任何单元格范围。 为了解决这个问题,您可以命名具有下拉列表的单元格,然后在Worksheet_Change
事件中,仅在范围名称匹配时评估Target
; 例如
If Target.Name = "MyDropDown" Then
' Evaluate the value of Target and update sheet3 as intended
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.