在我们处理Excel工作时,经常会遇到需要对excel的单元格或区域进行边框设置的情况。比如添加或者去除边框。下面我们探讨一下如何处理。
通过录制宏的方法添加边框
通过录制的宏的的方法添加边框:
1.点击开发工具
2.点击录制宏 3.选取要添加边框的区域
4.选择开始选项卡
5.选择边框下拉选框
6.选择所有边框线
7.结果如下
8.所录制的宏如下 Sub 宏2()
Range("D6:G7").Select
Range("G7").Activate
Selection.Borders(xlDiagonalDown).linestyle = xlNone
Selection.Borders(xlDiagonalUp).linestyle = xlNone
With Selection.Borders(xlEdgeLeft)
.linestyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.linestyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.linestyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.linestyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.linestyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.linestyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End Sub
可以看到通过录制宏的办法实现起来简单,但是代码复杂。
简单添加边框的办法
1.代码如下
Sub setborders()
Dim rng As Range
Set rng = Range("D6:G7")
With rng
.BorderAround xlDouble
.Borders(xlInsideVertical).linestyle = xlContinuous
.Borders(xlInsideHorizontal).linestyle = xlContinuous
End With
End Sub 2.运行结果如下:
可见第二种方法就简单多了。
删除所选区域的边框。
1.代码如下
Sub delborders()
Dim rng As Range
Set rng = Range("D6:G7")
rng.Borders.linestyle = xlNone
End Sub 2.运行结果如下:
我们可以通过一个程序来练习添加和删除边框
Sub borders_test()
setborders
MsgBox "按确认按钮删除边框!"
delborders
End Sub 运行结果如下:
好,以上就是我么对边框的一些处理,欢迎联系[email protected]