微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在VBA中格式化和重置excel单元格的格式

如何解决在VBA中格式化和重置excel单元格的格式

在下面的代码片段中,我在excel中使用了两个activex标签控件。 第一个标签(称为 lblCellAddress) 我根据下面给出的预设值格式化选定的单元格。 第二个标签(称为 lblFormateraser)我将单元格的格式设置为认值,因此它将所选单元格的格式转换为认值。

Private Sub lblCellAddress_Click()
    'Format the Cells
    '--------------------
    With Selection
        .Font.Size = 12
        .Font.Bold = True
        .Font.Color = vbBlue
        .Font.Name = "Calibri"
        .Columns.AutoFit
        .Interior.Color = vbGreen
        .Borders.Weight = xlThick
        '.Borders.Weight = xlThin
        .Borders.Color = vbRed
        '.BackgroundColor = vbRed
    End With
    
    'Format the Label CellFormater
    '--------------------
    With lblCellAddress
        .BackColor = vbRed
        .BorderColor = vbGreen
        .BorderStyle = fmBorderStyleSingle
        .ForeColor = vbBlue
        .Font = Calibri
        .Shadow = True
        .TextAlign = fmTextAlignCenter
    End With
                  
  '  Range("A20").Select
End Sub
    'Erase the Cell Formats
    '--------------------
Private Sub lblFormateraser_Click()
   With Selection
        .Font.Bold = False
        .Font.Color = vbBlack
        .Interior.Color = xlNone
        .Borders.Linestyle = xlNone
   End With
    
    'Format the Label CellFormater
    '--------------------
    With lblFormateraser
        .BackColor = vbGreen
        .BorderColor = vbRed
        .BorderStyle = fmBorderStyleSingle
        .ForeColor = vbBlue
        .Font = Calibri
        .Shadow = True
        .TextAlign = fmTextAlignCenter
    End With
End Sub

解决方法

Private Sub lblCellAddress_Click()
    'Format the Cells
    '--------------------
    With Selection
        .Font.Size = 12
        .Font.Bold = True
        .Font.Color = vbBlue
        .Font.Name = "Calibri"
        .Columns.AutoFit
        .Interior.Color = vbGreen
        .Borders.Weight = xlThick
        '.Borders.Weight = xlThin
        .Borders.Color = vbRed
        '.BackgroundColor = vbRed
    End With
    
    'Format the Label CellFormater
    '--------------------
    With lblCellAddress
        .BackColor = vbRed
        .BorderColor = vbGreen
        .BorderStyle = fmBorderStyleSingle
        .ForeColor = vbBlue
        .Font = Calibri
        .Shadow = True
        .TextAlign = fmTextAlignCenter
    End With
                  
  '  Range("A20").Select
End Sub
    'Erase the Cell Formats
    '--------------------
Private Sub lblFormatEraser_Click()
   With Selection
        .Font.Bold = False
        .Font.Color = vbBlack
        .Interior.Color = xlNone
        .Borders.LineStyle = xlNone
   End With
    
    'Format the Label CellFormater
    '--------------------
    With lblFormatEraser
        .BackColor = vbGreen
        .BorderColor = vbRed
        .BorderStyle = fmBorderStyleSingle
        .ForeColor = vbBlue
        .Font = Calibri
        .Shadow = True
        .TextAlign = fmTextAlignCenter
    End With
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。