用VBA为选定的单元格加上边框

Cells(1, 3).Select//当前选中的第一行第三列的单元格为例。 Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone 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

加边框之前最好用宏录制一下整个操作过程就了解流程了。

1.虽然用录制代码的方法可以很方便的获得设置单元格边框的代码,但可以看出,得出的代码非常复杂,实际上就是对每一步操作进行录制得来的。

2.利用Range对象的Borders属性及BorderAround方法分别可以设置单元格区域的内部线框及外部边框。

3.Borders属性的属性值及BorderAround方法的参数值可以复制刚才录制的代码而得。

下面是利用Range和borders的示例演示

Dim rng As Range

Set rng = Range("A1:B6")

With rng.Borders

.LineStyle = xlContinuous//边框设置

.ColorIndex = xlAutomatic//颜色设置

.TintAndShade = 0

.Weight = xlHairline//是否粗细

End With

rng.BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic

2:vba设置Excel单元格左对齐、右对齐、居中对齐、字体等

左对齐、右对齐、居中对齐

  ’选择区域或单元格右对齐    Selection.HorizontalAlignment = Excel.xlRight

  ’选择区域或单元格左对齐  Selection.HorizontalAlignment = Excel.xlLeft

  ’选择区域或单元格居中对齐    Selection.HorizontalAlignment = Excel.xlCenter

  固定区域的对齐方式的代码:

  Range("A1:D9").HorizontalAlignment = Excel.xlLeft

字体、字号、字型

  ’当前单元格字体为粗体  Selection.Font.Bold = True

  ’当前单元格字体为斜体  Selection.Font.Italic = True

  ’当前单元格字体为宋体20号字

  With Selection.Font   .Name = "雅黑体"   .Size = 20  End With

一个有信念者所开发出的力量,大于99个只有兴趣者。

用VBA为选定的单元格加上边框

相关文章:

你感兴趣的文章:

标签云: