Excel VBA 单元格区域边框处理

目录

在我们处理Excel工作时,经常会遇到需要对excel的单元格或区域进行边框设置的情况。比如添加或者去除边框。下面我们探讨一下如何处理。

通过录制宏的方法添加边框

通过录制的宏的的方法添加边框: 1.点击开发工具

image 2.点击录制宏 image 3.选取要添加边框的区域 image

4.选择开始选项卡 image

5.选择边框下拉选框

image

6.选择所有边框线

image

7.结果如下

image

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.运行结果如下:

image

可见第二种方法就简单多了。

删除所选区域的边框。

1.代码如下

    Sub delborders()
        Dim rng As Range
        Set rng = Range("D6:G7")
        rng.Borders.linestyle = xlNone
    End Sub 2.运行结果如下:

image

我们可以通过一个程序来练习添加和删除边框

Sub borders_test()
    setborders
    MsgBox "按确认按钮删除边框!"
    delborders
End Sub 运行结果如下:

image

好,以上就是我么对边框的一些处理,欢迎联系[email protected]

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦