简体   繁体   English

检测范围内的条件格式

[英]Detecting Conditional formatting within a range

I've been trying to set up a spreadsheet on excel that works a lot of calculations out but is also allowing for variables within it (ie the team numbers change sometimes).我一直在尝试在 excel 上设置一个电子表格,该电子表格可以进行大量计算,但也允许其中包含变量(即团队人数有时会发生变化)。

In this case I have 4 separate teams.在这种情况下,我有 4 个独立的团队。 Each team has it's own default conditional formatting which will set a default colour for that team.每个团队都有自己的默认条件格式,将为该团队设置默认颜色。 I've done this by just detecting if the cell is blank.我只是通过检测单元格是否为空白来完成此操作。

Example of teams团队示例

As you can see the default colours are blue and orange for shown 2 teams.如您所见,显示的 2 个团队的默认颜色是蓝色和橙色。

The issue I have is when someone copies & pastes from Orange to Blue team it takes on the formatting of said team.我遇到的问题是,当有人从橙色团队复制并粘贴到蓝色团队时,它会采用上述团队的格式。 For example if I copy and paste from Orange team where it says "COURSE" in yellow into the blue team, when I delete the word "COURSE" it should default back to blue but as it has been copied from Orange it defaults back to orange.例如,如果我从橙色团队复制并粘贴黄色“COURSE”到蓝色团队,当我删除“COURSE”这个词时,它应该默认回蓝色,但因为它是从橙色复制的,它默认回橙色.

I have managed to set the following which resets the formatting back to its original blue default (This is currently done from a button press for debug purposes).我设法设置了以下内容,将格式重置为其原始的蓝色默认值(目前这是为了调试目的通过按下按钮来完成的)。

Range(MyRange1).Select


Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=LEN(TRIM(D2))=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorLight2
    .TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = False

My issue is eventually I want to link this to a worksheet change to allow for copy & pasting.我的问题是最终我想将此链接到工作表更改以允许复制和粘贴。 But every time it runs it creates a duplicate version of the conditional formatting within the rules.但每次运行时,它都会在规则中创建条件格式的重复版本。

So I need to delete any other instances of the condition that previously exists, otherwise overtime they will stack up.所以我需要删除以前存在的条件的任何其他实例,否则超时它们会堆积起来。

I know it's going to be an IF/THEN statement but for the life of me I cannot think how to test for it.我知道这将是一个 IF/THEN 语句,但对于我的生活,我无法考虑如何测试它。

I have severe writer's block at the moment so I really hope this makes sense!我目前有严重的作家障碍,所以我真的希望这是有道理的!

This will cycle through all the conditional formats applied in MyRange1 and delete any that are not priority one.这将循环使用MyRange1应用的所有条件格式,并删除任何不是优先级的格式。

For Each f In MyRange1.FormatConditions
    If f.Priority <> 1 Then f.Delete
Next

Sorry, just noticed that you use MyRange1 as the range address, so you'd use:抱歉,刚刚注意到您使用MyRange1作为范围地址,因此您可以使用:

For Each f In Range(MyRange1).FormatConditions
    If f.Priority <> 1 Then f.Delete
Next

Could you just delete the old conditions and then reinstate them every time you copy and paste?您可以删除旧条件,然后在每次复制和粘贴时恢复它们吗?

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)

Range("A1:A100").Select

With Selection.FormatConditions.Delete
End With

Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=LEN(TRIM(D2))=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorLight2
    .TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM