VBA 单元格字体设置

Option Explicit

Sub Font_1()
    Dim r As Long, c As Long, i As Integer, num As Integer, str As String
    Dim myRange As Range
    Dim myFon As Font
    Set myRange = ActiveSheet.UsedRange
    myRange.ClearFormats
    r = myRange.Rows.Count
    c = myRange.Columns.Count
    
    '查找语文成绩所在列号
    For i = 1 To c
        If myRange.Cells(1, i) = "语文" Then
            num = i
            With myRange.Cells(1, i)
                .RowHeight = Application.CentimetersToPoints(2)
                .ColumnWidth = Application.CentimetersToPoints(1)
                .VerticalAlignment = xlCenter  'xlRight  xlLeft  xlDistributed  xlGeneral
                .HorizontalAlignment = xlCenter  'xlRight  xlLeft  xlDistributed  xlGeneral
            End With
            MsgBox "语文成绩所在列号为: " & num
        End If
    Next
    
    '对语文成绩所在列进行条件筛选,然后改变其单元格文字格式
    For i = 2 To r
        If myRange.Cells(i, num) < 90 And myRange.Cells(i, num) _
            <> "" Then
            Debug.Print myRange.Cells(i, num)
            Set myFon = myRange.Cells(i, num).Font
            With myFon
                .Name = "楷体"
                .Size = 15
                .Bold = True
                .Italic = True
                .Color = RGB(255, 0, 0)
                .Strikethrough = True '水平删除线
                .Underline = xlUnderlineStyleNone 'xlUnderlineStyleSingle 'xlUnderlineStyleDouble
                '.Shadow = False  是/否无变化??
                .Subscript = False
                .Superscript = False
                '具体属性设置参看:https://docs.microsoft.com/zh-cn/office/vba/api/excel.xlpattern
            End With
            
            With myRange.Cells(i, num)   '设置居中对齐
                .VerticalAlignment = xlCenter  'xlRight  xlLeft  xlDistributed  xlGeneral
                .HorizontalAlignment = xlCenter  'xlRight  xlLeft  xlDistributed  xlGeneral
            End With
            
            myRange.Cells(i, num).Select
        End If
    Next
    
    '查找英语成绩所在列号
    For i = 1 To c
        If myRange.Cells(1, i) = "英语" Then
            num = i
            With myRange.Cells(1, i)
                .RowHeight = Application.CentimetersToPoints(2)
                .ColumnWidth = Application.CentimetersToPoints(1)
                .VerticalAlignment = xlCenter  'xlRight  xlLeft  xlDistributed  xlGeneral
                .HorizontalAlignment = xlCenter  'xlRight  xlLeft  xlDistributed  xlGeneral
            End With
            MsgBox "英语成绩所在列号为: " & num
        End If
    Next
    
    '对英文成绩所在列的单元格数据格式进行设置
    For i = 2 To r
        If myRange.Cells(i, num) <> "" Then
            Debug.Print myRange.Cells(i, num)
            With myRange.Cells(i, num)   '设置居中对齐
                .VerticalAlignment = xlCenter  'xlRight  xlLeft  xlDistributed  xlGeneral
                .HorizontalAlignment = xlCenter  'xlRight  xlLeft  xlDistributed  xlGeneral
                .NumberFormat = "###.00"
            End With
            myRange.Cells(i, num).Select
        End If
    Next
End Sub
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值