多语言展示
当前在线:1872今日阅读:97今日分享:45

EXCEL VBA中非常重要的语句用法——with语句

利用with语句实现表格边框的绘制。
工具/原料
1

电脑

2

Excel

方法/步骤
1

编写绘制边框的程序:'绘制置单元格边框Public Sub unitBorder()    With Selection.Borders(xlEdgeLeft) '设置左边框        .LineStyle = xlContinuous        .Weight = xlThin        .ColorIndex = xlAutomatic    End With    With Selection.Borders(xlEdgeTop) '设置上边框        .LineStyle = xlContinuous        .Weight = xlThin        .ColorIndex = xlAutomatic    End With    With Selection.Borders(xlEdgeBottom) '设置下边框        .LineStyle = xlContinuous        .Weight = xlThin        .ColorIndex = xlAutomatic    End With    With Selection.Borders(xlEdgeRight) '设置右边框        .LineStyle = xlContinuous        .Weight = xlThin        .ColorIndex = xlAutomatic    End WithEnd Sub

2

运行以上程序可以对选择的单元格绘制边框。编写下面程序进行示范:Sub drawingborder() Sheets('Sheet1').Select Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets('sheet1') Range(Cells(2, 1), Cells(4, 1)).Select: unitBorderEnd Sub

3

运行以上程序,在A2至A4单元格绘制边框。结果如下:

4

利用with语句可以简化表格绘制程序的编写,对于制表类的程序编写非常有用。

推荐信息