VBA Excel实践如何系列(三)vba如何设置单元格边框、字体

vba设置单元格字体,以下是设置给定单元格内字体的加粗,单元格填充灰色等,参数是Range对象

Sub font_sty(Rng As Range)
    Rng.Select
    Selection.Font.Bold = True
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.349986266670736
        .PatternTintAndShade = 0
    End With
End Sub

vba设置单元格边框,以下是设置给定单元格边框,参数是Range对象

Sub sty_sig(Rng As Range)
    Rng.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

对这两个方法(片段)进行调用

我们使用《如何系列(二)》中的例子,最后增加两个call 分别call设置单元格字体格式,和设置单元格边框

Sub sqltest()
    Dim Spath As String
    Spath = ThisWorkbook.Path & "\pbxtest.xlsx"
    Set adConn = New ADODB.Connection
    '
    adConn.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties=excel 12.0;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';data source=" & Spath & ""
    Set rs = adConn.Execute("Select 编号,测试环境,测试项目 From [sheet1$a1:j35] ")
    '
    sht_name = "sheet3"
    Set sht = ThisWorkbook.Worksheets(sht_name)
    '
    For i = 1 To rs.Fields.Count Step 1
        sht.Range("A1").Offset(0, i - 1) = rs.Fields(i - 1).Name '
    Next i
    '
    sht.Range("A2").CopyFromRecordset rs
    sht.Cells.EntireColumn.AutoFit
    Call sty_sig(Range(Cells(1, 1), Cells(13, 3)))
    Call font_sty(Range(Cells(1, 1), Cells(1, 3)))
End Sub

结果:

之前的:

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值