如何使用VBA创build条件边界?

基本上,我想做一个代码,根据内容selectExcel的单元格。 我发现很难用言语解释,所以我上传了这个图片。

在这里输入图像说明

我想用红色边框select所有包含复选标记的行。 但是如果有一个带有复选标记的行跟着另一个带有复选标记,就做一个大的select。 (就像我在图片中手动的那样)

任何解决scheme

提前致谢

我知道我在评论中另有说法,但现在我已经考虑了一些,这是条件格式完全可以实现的。

这个想法是,你会事先设置所有的边界,然后使用条件格式来删除你不想要的边界。

首先,当没有复选标记时,为每一行设置边界的方式。

在这里输入图像说明

当“通过”列是“否”时,您想要删除左边框和右边框。 使用公式规则设置条件格式来实现此目的。 请确保lockingD列,因为该公式正在应用于多列,并且您总是要查看D列。

在这里输入图像说明

如果当前行在传递列中有“是”,并且下面的行中也有“是”,则要删除底部边框。 您可以使用其他条件格式来执行此操作。

在这里输入图像说明

您需要用附加的条件公式来涵盖更多的情况,但这是一般的想法。

在这里输入图像说明

我的做法是这样的:首先写一个在任何范围的外面放一个红色边框的子。 它应该看起来像这样:

Sub ApplyBorder(inputRg as Range) Call inputRg.BorderAround(Weight:=xlMedium, Color:=vbRed) End Sub 

现在我们可以遍历我们的范围。 如果我们find一张支票,请将其包括在范围内给予一个边框。 如果我们find一个x,然后应用我们有的范围内的红色边框,并将其重置为空。

 Sub FormatTable() Dim AllTable As Range, oneRedRg As Range, oneRow As Range Dim iRow As Integer Set AllTable = GetTable For iRow = 1 To AllTable.Rows.Count Set oneRow = AllTable.Rows(iRow) If oneRow.Cells(1, 3) = True Then If oneRedRg Is Nothing Then 'if our current redRg is unset, Set oneRedRg = oneRow 'then this is the first one, so set it Else Set oneRedRg = Range(oneRedRg, oneRow) 'if it is already set, then expand oneRedRg to include this one End If Else if Not oneRedRg Is Nothing Then Call ApplyBorder(oneRedRg) 'if we find a range that need not be red, then Set oneRedRg = Nothing 'apply the border to our redRg and reset it to nothing End If Next iRow If Not oneRedRg Is Nothing Then Call ApplyBorder(oneRedRg) 'this last line captures a group of reds 'that are at the end of the table. End Sub 

我已经排除了“GetTable”函数。 在我的答案,GetTable返回整个表迭代通过,没有头。 另外请注意,为了方便起见,我在电子表格中将检查和x的值更改为“True”或“False”。

快乐编码!

有这四个规则的CF可以服务,其中不可见的是蜱的代码点(也许是'252'):

SO23619627的例子

并根据需要调整范围以适应。