区域也是Excel工作表的基本操作对象,其操作也比较简单,看下面的代码
Sub test()
Range("A1:B2").Select '选中A1到B2单元格
Range("A1:B2").Interior.Color = vbRed '设置A1到B2单元格的底色为红色
Range("A1:B2").Merge '合并A1到B2单元格
End Sub
下面的代码是设置边框用的
Sub Macro1()
Range("A1:B2").Select
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
代码中selection代表表格中被选中的区域,在上面的代码中即range("A1:B2")。对于一些设置格式的代码通常是不用自己来写的,只要录制一个宏就可以了,关于怎么录制宏请参见录制宏。
联系客服