用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





  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值