下面以一个“用RefEdit实现的设置单元格格式窗体”的实例来解释选项按钮的利用。
1 新建窗体并插入如下控件:2 编写调用窗体过程Sub 设置字体格式()
frmFontsize.Show
End Sub
3 编写窗体代码Private Sub UserForm_Initialize()
With cmbFont
.AddItem \"大众黑体\"大众
.AddItem \公众楷体\"大众
.AddItem \"大众仿宋_GB2312\"大众
.Value = .List(0)
End With
For i = 8 To 72
cmbSize.AddItem i
Next
cmbSize.Value = 8
End Sub
Private Sub chkBold_Click() '预览字形(粗体)
lblPreview.Font.Bold = chkBold.Value
End Sub
Private Sub chkItalic_Click() '预览字形(斜体)
lblPreview.Font.Italic = chkItalic.Value
End Sub
Private Sub cmbFont_Change() '预览字体
lblPreview.Font.Name = cmbFont.Value
End Sub
Private Sub cmbSize_Change() '预览字号
lblPreview.Font.Size = cmbSize.Value
End Sub
Private Sub cmdSet_Click()
Dim rng As Range, i As Integer, str1 As String
If RefEdit1.Value = \公众\公众 Then '未选择单元格区域
MsgBox \"大众请先在事情表区域拖动鼠标,选择一个单元格区域!
\公众, vbCritical + vbOKOnly
Exit Sub
End If
i = InStr(1, RefEdit1.Value, \"大众!\"大众) '去掉事情表名
str1 = Mid(RefEdit1.Value, i + 1)
Set rng = Range(str1) '获取对单元格区域的引用
With rng.Font '设置格式
.Name = cmbFont.Value '字体
.Size = cmbSize.Value '字号
.Bold = chkBold.Value '粗体
.Italic = chkItalic.Value '斜体
End With
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
在打开窗体后,即可进行如下操作:
-End-