![Excel 中某一区域的所有边框均等效](https://linux22.com/image/1486602/Excel%20%E4%B8%AD%E6%9F%90%E4%B8%80%E5%8C%BA%E5%9F%9F%E7%9A%84%E6%89%80%E6%9C%89%E8%BE%B9%E6%A1%86%E5%9D%87%E7%AD%89%E6%95%88.png)
我认为 VBA 中有一种方法可以为某个范围内的每个单元格设置 4 个墙边框,但我还没有想出或找到一种方法。基本上,我希望 (A1:R780) 等范围都有自己的方块。
答案1
我不知道还有比这更有效的方法。
With Range("A1:R780")
.Borders(xlInsideVertical).LineStyle = xlContinuous
.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.BorderAround xlContinuous
End with
但我相信这也会起作用。
Range(“A1:R780").Borders.LineStyle = xlContinuous
答案2
我是如何做到的... 错误行是针对区域没有垂直线或水平线的情况。老实说,最初我没有使用 BorderAround,而是使用了 xlEdgeBottom、xlEdgeTop、xlEdgeLeft、xlEdgeRight。
我不得不使用 5 次调用,也就是将其作为子程序的原因,而且它们是动态的。
示例通话
Call BoxIt(Range("A1:z25"))
子程序
Sub BoxIt(aRng As Range)
On Error Resume Next
With aRng
'Clear existing
.Borders.LineStyle = xlNone
'Apply new borders
.BorderAround xlContinuous, xlThick, 0
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.Weight = xlMedium
End With
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.Weight = xlMedium
End With
End With
End Sub