如何用VBA实现对单元格增加边框,以及单元格内容居中等格式设置。

如何用VBA实现对单元格增加边框,以及单元格内容居中等格式设置。,第1张

使用VBA可以轻松的设置Excel中的一个或多个单元格甚至是一个区域的或者是被选中单元格的左对齐、友对齐、居中对齐、字体、字号、字型等属性。

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

'选择区域或单元格右对齐Selection.HorizontalAlignment

=

Excel.xlRight

'选择区域或单元格左对齐Selection.HorizontalAlignment

=

Excel.xlLeft

'选择区域或单元格居中对齐Selection.HorizontalAlignment

=

Excel.xlCenter

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

Range("A1:A9").HorizontalAlignment

=

Excel.xlLeft

②字体、字号、字型

'当前单元格字体为粗体Selection.Font.Bold

=

True

'当前单元格字体为斜体Selection.Font.Italic

=

True

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

With

Selection.Font

 .Name

=

"宋体"

 .Size

=

20

End

With

Sub 设置框线()

    Dim x, y

    x = ActiveSheet.UsedRange.Rows.Count '使用的行数

    y = ActiveSheet.UsedRange.Columns.Count '使用的列数

    x = WorksheetFunction.RoundUp(x / 12, 0) * 12 '按12对齐

    With ActiveSheet.UsedRange.Resize(x, y).Borders

        .LineStyle = xlContinuous

        .Weight = xlThin

    End With

End Sub

只要一句代码?那就给你一句参考一下吧:picCount = ActiveDocument.InlineShapes.Count '计算文件中图片数目在Word中,插入的图片已被转化为 InlineShape 对象。之后用For循环语句,给所有图片加黑色边框。单个图片加边框的语句,你自己可以录制一个宏看看,将录制的宏代码拷贝到For循环中修改一下即可。

___________________________________________________________________

单个图片加边框,你自己录制宏就可以看到代码了,这是学习VBA的必由之路啊。选中一张图片,工具-宏-录制新宏,然后,格式-边框和阴影,给图片加上黑边框,然后,alt+F11打开VB编译器,就看到代码了。本想只授人以渔即足够了,但犹豫了一下,还是贴给你吧:

With Selection.InlineShapes(1)

With .Borders(wdBorderLeft)

.LineStyle = wdLineStyleSingle

.LineWidth = wdLineWidth050pt

.Color = wdColorAutomatic

End With

With .Borders(wdBorderRight)

.LineStyle = wdLineStyleSingle

.LineWidth = wdLineWidth050pt

.Color = wdColorAutomatic

End With

With .Borders(wdBorderTop)

.LineStyle = wdLineStyleSingle

.LineWidth = wdLineWidth050pt

.Color = wdColorAutomatic

End With

With .Borders(wdBorderBottom)

.LineStyle = wdLineStyleSingle

.LineWidth = wdLineWidth050pt

.Color = wdColorAutomatic

End With

.Borders.Shadow = False

End With

With Options

.DefaultBorderLineStyle = wdLineStyleSingle

.DefaultBorderLineWidth = wdLineWidth050pt

.DefaultBorderColor = wdColorAutomatic

End With

计算图片数目是为了For循环用的,有多少图片就要循环多少次,给所有图片都加上边框。

For i = 1 to picCount

......

Next i

中间就是上面那段代码,把InlineShapes(1)改为InlineShapes(i)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11764581.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存