我试图让我的代码工作,如果用户键入" 1"在单元格D19内,然后单元格F2和F3的背景颜色变为红色,如果它们键入" 2"颜色变为黄色。我无法理解为什么我的代码不起作用,任何有用的东西。感谢。
<DataTemplate x:Key="CommentTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ItemsControl Grid.Column="0" ItemsSource="{Binding ParentDepthList}" HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Stroke="{ThemeResource SystemControlForegroundBaseHighBrush}"
VerticalAlignment="Stretch" Opacity="0.2" Width="1" StrokeThickness="1"
StrokeDashArray="1,2" StrokeDashCap="Round" Margin="{StaticResource BigRightMargin}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Border BorderThickness="0 0 0 1 " Grid.Column="1" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
Margin="{StaticResource MediumLeftMargin}">
<StackPanel>
<controls:MarkdownTextBlock Text="{Binding Comment.Content}" VerticalContentAlignment="Center"
HorizontalContentAlignment="Center" VerticalAlignment="Center"
micro:Message.Attach="[Event LinkClicked] = [Action OpenLink($eventArgs)]"
Margin="{StaticResource SmallLeftMargin}"
LinkForeground="{ThemeResource SystemControlForegroundBaseHighBrush}" />
<StackPanel Orientation="Horizontal" Margin="{StaticResource MediumAllMargin}">
<HyperlinkButton
micro:Message.Attach="[Event LinkClicked] = [Action OpenLink($eventArgs)]"
NavigateUri="{Binding Comment.Name, Converter={StaticResource UserTextConverter}}"
Content="{Binding Comment.Name}" />
<TextBlock Margin="10 0 0 0" VerticalAlignment="Center">-</TextBlock>
<TextBlock VerticalAlignment="Center" Text="{Binding Comment.Created}"
Margin="{StaticResource MediumLeftMargin}" />
</StackPanel>
</StackPanel>
</Border>
</Grid>
答案 0 :(得分:3)
我还建议使用条件格式。但是,要回答所提出的问题:将其放入具有这些范围的工作表的工作表模块中。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D19")) Is Nothing Then
Select Case Range("D19").Value
Case 1
Range("F2:F3").Interior.Color = vbRed
Case 2
Range("F2:F3").Interior.Color = vbYellow
End Select
End If
End Sub
我测试了这段代码并且运行良好。如果您希望在D19
的值不是1或2时删除着色,那么您也可以包含Case Else
语句。
答案 1 :(得分:2)
条件格式化会更好,这样每次用户在D19单元格中键入时,Excel都会修改F2:F3背景颜色
Sub ColorColumns()
Dim r As Range
Set r = Range("F2:F3")
r.FormatConditions.Delete
r.FormatConditions.Add Type:=xlExpression, Formula1:="=$D$19=1"
r.FormatConditions(r.FormatConditions.Count).SetFirstPriority
With r.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 244
.TintAndShade = 0
End With
r.FormatConditions(1).StopIfTrue = False
r.FormatConditions.Add Type:=xlExpression, Formula1:="=$D$19=2"
r.FormatConditions(r.FormatConditions.Count).SetFirstPriority
With r.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
r.FormatConditions(1).StopIfTrue = False
End Sub
请注意"=$D$19=2"
r.FormatConditions.Add Type:=xlExpression, Formula1:="=$D$19=2"