简体   繁体   English

Excel VBA,如何使用一个工作表上的下拉列表中的选择格式(颜色)另一工作表上的一系列单元格

[英]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. 您需要进行几次更改。

  1. You'll need to leverage the Worksheet_Change event on your sheet1 to update your sheet3. 您需要利用sheet1上的Worksheet_Change事件来更新sheet3。
  2. In your code, you'll need to refer to sheet3 directly because you won't be able to select it from the sheet1 worksheet event. 在您的代码中,您将需要直接引用sheet3,因为您将无法从sheet1工作表事件中选择它。
  3. Using the 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.

相关问题 Excel VBA用于隐藏一个工作表中的单元格(如果它们匹配另一个工作表中的单元格) - Excel VBA for hiding cells in one sheet if they match cells in another sheet vba表颜色范围格式 - vba sheet color range Format 如何在Excel VBA中将hlookup与另一张工作表中的一系列数据一起使用? - how to use hlookup in Excel VBA with a range of data from another sheet? Excel VBA 2016将范围单元格传递到另一个工作表 - Excel VBA 2016 Passing range cells to another sheet as calculated 如何使用 VBA 将一张纸上的 3 个单元格组合成另一张纸上的 1 个单元格? - How to use VBA to combine 3 cells from one sheet into 1 cell in another sheet? Excel-将单元格+宏+ VBA从一张纸复制到另一张纸? - Excel - copy cells+macro+VBA from one sheet to another? Excel VBA无需选择即可从一张纸复制到另一张纸 - Excel VBA copying from one sheet to another without selection 使用VBA将一个Excel工作表的格式复制到另一个Excel工作表 - Copying Format of one excel sheet to another excel worksheet using VBA excel vba 范围从一张纸复制到另一张纸(错误 1004) - excel vba range copy from one sheet to another (error 1004) 我们可以使用单元格范围验证从 EPPLUS 的另一张表中填充 excel 中的下拉列表吗 - Can we use Cell Range Validation to populate a dropdown list in excel from another sheet in EPPLUS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM