[英]Excel VBA, How to use the selection in a dropdown list on one sheet format(color) a range of cells on another sheet
From what I've gathered, conditional formatting can't be done across sheets in a workbook so what I'm trying to do now is use VBA. 从我收集到的信息来看,条件格式化无法在工作簿的各个工作表之间完成,因此我现在要尝试的是使用VBA。
I would like a word (let's say "yes" or "no") selected in a dropdown list on sheet1! 我想在sheet1的下拉列表中选择一个词(比如说“是”或“否”)! to then color a range of cells;
然后为一系列单元着色; Range("J3:P29") on sheet3!.
在sheet3!上的范围(“ J3:P29”)。 If "yes" is selected the range will be colored and if "no" is selected then the range will not be colored.
如果选择“是”,则范围将被着色;如果选择“否”,则范围将不被着色。
I used the macrorecorder to record the code below. 我使用了macrorecorder来记录下面的代码。 However, it doesn't record the selection from the dropdown list.
但是,它不会记录下拉列表中的选择。 It just replaced the action with commas.
它只是用逗号代替了动作。
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
I would greatly appreciate some help. 我将不胜感激一些帮助。 Thanks!
谢谢!
Couple changes you need to make. 您需要进行几次更改。
Worksheet_Change
event on your sheet1 to update your sheet3. Worksheet_Change
事件来更新sheet3。 Target
Value, evaluate whether or not it meets your specified condition and update sheet3 as desired Target
,评估它是否满足指定条件,并根据需要更新sheet3 Open the VB editor and open the Sheet1 code. 打开VB编辑器,然后打开Sheet1代码。 Then paste the following code.
然后粘贴以下代码。
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
Note that Target
will capture any range of cells that is changed on the sheet. 请注意,
Target
将捕获工作表上更改的任何单元格范围。 to counter that, you can name the cell that has your dropdown, and then in the Worksheet_Change
event, only evaluate Target
if the name of the range is matched; 为了解决这个问题,您可以命名具有下拉列表的单元格,然后在
Worksheet_Change
事件中,仅在范围名称匹配时评估Target
; for example 例如
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.