【问题标题】:Creating a border around cells with the same value在具有相同值的单元格周围创建边框
【发布时间】:2010-06-04 18:53:54
【问题描述】:
我有一张像下面这样的桌子。如何让 Excel 在第 4 列中为具有相同数字的组设置边框,以便在组周围有边框。我在想条件格式可以做到这一点,但我想不出怎么做。所以我认为唯一的选择是宏。有人可以帮忙吗?
1 64436 549419 1
2 64437 549420 1
3 64438 549421 1
4 64439 549422 1
5 64440 549423 1
6 64441 549424 1
7 64442 549425 1
8 64443 549426 1
9 64444 549427 1
10 64445 549428 1
11 64446 549429 1
12 64447 549430 1
13 64448 549431 2
14 64449 549432 2
15 64450 549433 2
16 64451 549434 2
17 64452 549435 2
18 64453 549436 2
19 64454 549437 2
20 64455 549438 2
21 64456 549439 2
22 64457 549440 4
23 64458 549441 4
24 64459 549442 5
25 64460 549443 5
26 64461 549444 5
27 64462 549445 5
28 64463 549446 5
29 64464 549447 5
30 64465 549448 6
31 64466 549449 6
32 64467 549450 6
33 64468 549451 6
34 64469 549452 6
35 64470 549453 6
36 64471 549454 6
37 64472 549455 9
38 64473 549456 9
39 64474 549457 9
【问题讨论】:
标签:
excel
ms-office
excel-2003
conditional-formatting
vba
【解决方案1】:
您需要使用相对引用。
- 选择要对其进行条件格式设置的列范围。
- 在各自的条件下输入以下三个公式:
- =AND($C2=$C3,$C3=$C4)
- =AND($C2$C3,$C3=$C4)
- =AND($C2=$C3,$C3$C4)
- 根据需要设置它们的格式。
将所有“$C”替换为“${Your Column}”。请注意,这不会在单个项目周围放置任何边框,因为您在选择中不能再有三个条件格式条件。
【解决方案2】:
我看不到一个简单的非宏观解决方案来完全满足您的需求,但 PowerUser 的解决方案似乎还不错。
这是一个基于宏的解决方案,它将在最后一列中具有相同数字的行周围设置边框。我假设您的数据在 A:D 列中。
要使用此宏,只需单击列表中的任何单元格,然后触发宏。
作为快速指南:
-
AddBorders 是主要的宏,它简单地遍历最后一列中的所有单元格,并在合适的边框时计算出来
-
AddBorder 是一个添加边框的简短例程。
-
作为奖励,AddBorder 从 Excel 的 56 种调色板中随机选择一种颜色,这样您的每个边框都是不同的颜色,以便于查看
Sub AddBorders()
Dim startRow As Integer
Dim iRow As Integer
startRow = 1
For iRow = 2 To ActiveCell.CurrentRegion.Rows.Count
If WorksheetFunction.IsNumber(Cells(iRow + 1, 4)) Then
If Cells(iRow, 4) <> Cells(iRow - 1, 4) Then
AddBorder startRow, iRow - 1
startRow = iRow
End If
Else
AddBorder startRow, iRow
End If
Next iRow
End Sub
Sub AddBorder(startRow As Integer, endRow As Integer)
Dim borderRange As Range
Dim randomColor As Integer
randomColor = Int((56 * Rnd) + 1)
Set borderRange = Range("A" & startRow & ":D" & endRow)
borderRange.BorderAround ColorIndex:=randomColor, Weight:=xlThick
End Sub
【解决方案3】:
我提出了这个解决方案,它在我的 Excel 2010 上运行很奇怪:/
我无法在 2003 年对其进行测试,所以请告诉我是否可以。
Sub PaintBorder()
Dim iRow As Integer
iRow = 1
Dim strTemp As String
strTemp = Range("D" & iRow).Value
Dim strPrev As String
Dim sectionStart As Integer
sectionStart = 1
Do
strPrev = strTemp
strTemp = Range("D" & iRow).Value
If strPrev <> strTemp Then
ActiveSheet.Range(Cells(sectionStart, 1), Cells(iRow - 1, 4)).BorderAround xlSolid, xlMedium, xlColorIndexAutomatic
sectionStart = iRow
End If
iRow = iRow + 1
Loop Until strTemp = vbNullString
End Sub
【解决方案4】:
您只是想让它对人眼更具可读性吗?如果是这样,我建议交替使用背景颜色。例如,每次第 4 列中的数字发生变化,背景颜色就会从白色变为蓝色,反之亦然。我一直这样做:
添加一个额外的列 E。由于您的参考列是 D,请输入:
=MOD(IF(D5D4,E4+1,E4),2)
(即如果这一行的列 D 与最后一行的 D 不同,则从 0 更改为 1 或从 1 更改为 0)
隐藏列,使最终用户看不到它。
制作 2 个条件公式。如果您的隐藏值为 0,第一个会将行颜色更改为白色。如果您的隐藏值为 1,第二个会将其更改为蓝色。
没有宏。没有 VBA 编码。只有 1 个隐藏列和一些条件公式。即使您的 D 列跳过数字,颜色仍应正确交替:)
(我每天都在 XL 2003 上使用它。我希望它适用于 2007 年)