あなたはこの後ことがあります、より効果的にリファクタリングすることができ
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Address <> "$B$2" Then Exit Sub '<--| exit if "changed" cell is not "B2"
With Sh '<--| reference sheet with "changed" cell
Select Case .Range("B2").Value '<--| act with respect to B2 cell current value
Case "Rose"
With .Range("Header").Interior '<--| reference the specific named range instead of using 'Application.GoTo' method and 'Selection' object
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
With .Range("Row").Interior '<--| reference the specific named range instead of using 'Application.GoTo' method and 'Selection' object
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
With .Range("Fill").Interior '<--| reference the specific named range instead of using 'Application.GoTo' method and 'Selection' object
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End Select
End With
End Sub
:
Option Explicit
Private Sub workbook_sheetchange(ByVal Sh As Object, ByVal Target As Range)
If Target.Address <> "$B$2" Then Exit Sub '<--| exit if "changed" cell is not "B2"
With Sh '<--| reference sheet with "changed" cell
Select Case .Range("B2") '<--| act with respect to B2 cell current value
Case "Rose"
FormatCell Union(.Range("Header"), .Range("Row"), .Range("Fill")), _
xlSolid, _
xlAutomatic, _
xlThemeColorAccent6, _
-0.249977111117893, _
0 '<--| reference all listed named ranges and format their 'Interior' object with passed properties
.Range("Fill").Interior.TintAndShade = 0.599993896298105 '<--| change only "Fill" named range 'Interior' 'TintAndShade' property
End Select
End With
End Sub
Sub FormatCell(cell As Range, pttrn As XlPattern, pttrnClrIndx As XlColorIndex, thmClr As XlThemeColor, tntAndShd As Single, pttrnTntAndShd As Variant)
With cell.Interior
.pattern = pttrn
.PatternColorIndex = pttrnClrIndx
.ThemeColor = thmClr
.TintAndShade = tntAndShd
.PatternTintAndShade = pttrnTntAndShd
End With
End Sub
ありがとうございました! –
ようこそ。私の答えがあなたの質問を解決したら、それを合格とマークしてください。 – user3598756
ありがとう、このコードはうまくいきます。私はあなたの答えを「受け入れる」ことを望みますが、私はそれを行う方法がわかりません(私はリンクが見えません)?これも私に助けてくれますか? –