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
推荐阅读
- html - 如何在卡片中正确拟合带有模糊的背景图像?
- sharepoint - SharePoint Workflow - 通过 SharePoint Workflow 删除或更新 PDF 文件
- php - 我应该如何设置 PhpStorm 以便使用 Docker 调试微服务?
- reactjs - 在 React JS 中使用分页导航到下一页时清除状态并使用 newdata 更新
- vega-lite - 有没有办法编写自定义排序函数来对 vegalite 中的列中的数据进行排序?
- c# - Idiomatic way to implement atomic web service call
- sql - 如何在 Access 中制作整行的多个副本?
- haskell - 将 f 应用于每个元素并将结果作为列表返回
- c# - 如果空间不足,删除按钮文本
- amazon-web-services - 从 AWS Kinesis 上的外部 API 推送数据