EXCEL VBA中非常重要的语句用法——with语句
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 With
End Sub

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

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


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