![](/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.