现在让我们看一个练习使用控件的小练习 (Controls)。
有一个包含现成表单和控制元素的文件。我们需要编写使所有元素正常工作的代码,以便用户可以通过与表单交互来填充我们的临时数据库。
以下是要下载的 Excel 文件: controls_exercise.xls
现在应该很清楚,这里的目标是使用表单填充表。
需要考虑的几点:
首先,我们需要将表单的 Zoom 属性增加到 120 以使其更易于使用:
我们已经介绍了检查选项按钮(在有关控件的第一课中),因此这里我们使用一个简单的解决方案。
默认选择“Mrs”(属性Value: True),即我们在选择时不会检查地址。
Private Sub CommandButton_Close_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize() '表单打开时加载列表
For i = 1 To 252 '从国家/地区工作表生成 252 个国家/地区的列表
ComboBox_Country.AddItem Sheets("Country").Cells(i, 1)
Next
End Sub
一个简单的解决方案是,如果任何控件为空,则显示一个对话框。
Private Sub CommandButton_Add_Click()
If TextBox_Last_Name.Value = "" Or TextBox_First_Name.Value = "" Or TextBox_Address.Value = "" Or TextBox_Place.Value = "" Or ComboBox_Country.Value = "" Then
MsgBox "Form incomplete"
Else
'此处输入联系人的说明...
End If
End Sub
但要让它变得更复杂一点,应该单独检查每个元素,如果其中任何一个元素为空,则其 name () 的颜色应更改为红色:
Private Sub CommandButton_Add_Click()
'将标题颜色设置为黑色
Label_Last_Name.ForeColor = RGB(0, 0, 0)
Label_First_Name.ForeColor = RGB(0, 0, 0)
Label_Address.ForeColor = RGB(0, 0, 0)
Label_Place.ForeColor = RGB(0, 0, 0)
Label_Country.ForeColor = RGB(0, 0, 0)
'内容控制
If TextBox_Last_Name.Value = "" Then '如果没有指定任何内容...
Label_Last_Name.ForeColor = RGB(255, 0, 0) '将标题颜色设置为红色
ElseIf TextBox_First_Name.Value = "" Then
Label_First_Name.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Address.Value = "" Then
Label_Address.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Place.Value = "" Then
Label_Place.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_Country.Value = "" Then
Label_Country.ForeColor = RGB(255, 0, 0)
Else
'此处输入联系人的说明...
End If
End Sub
应将以下代码插入到上面代码所示的位置(见注释):
Dim row_number As Integer, salutation As String
'选择上诉
For Each salutation_button In Frame_Salutation.Controls
If salutation_button.Value Then
salutation = salutation_button.Caption '已选择上诉
End If
Next
'row_number = 列中最后一个非空单元格的行号 +1
row_number = Range("A65536").End(xlUp).Row + 1
'将值插入工作表
Cells(row_number, 1) = salutation
Cells(row_number, 2) = TextBox_Last_Name.Value
Cells(row_number, 3) = TextBox_First_Name.Value
Cells(row_number, 4) = TextBox_Address.Value
Cells(row_number, 5) = TextBox_Place.Value
Cells(row_number, 6) = ComboBox_Country.Value
'粘贴后返回原来的值
OptionButton1.Value = True
TextBox_Last_Name.Value = ""
TextBox_First_Name.Value = ""
TextBox_Address.Value = ""
TextBox_Place.Value = ""
ComboBox_Country.ListIndex = -1
就是这样,这里有完整的练习代码和要下载的文件:
Private Sub CommandButton_Close_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize() '“国家/地区”表中的 252 个国家/地区列表
For i = 1 To 252
ComboBox_Country.AddItem Sheets("Country").Cells(i, 1)
Next
End Sub
Private Sub CommandButton_Add_Click()
'将名称颜色设置为黑色
Label_Last_Name.ForeColor = RGB(0, 0, 0)
Label_First_Name.ForeColor = RGB(0, 0, 0)
Label_Address.ForeColor = RGB(0, 0, 0)
Label_Place.ForeColor = RGB(0, 0, 0)
Label_Country.ForeColor = RGB(0, 0, 0)
'内容控制
If TextBox_Last_Name.Value = "" Then '如果没有指定任何内容...
Label_Last_Name.ForeColor = RGB(255, 0, 0) '将标题颜色设置为红色
ElseIf TextBox_First_Name.Value = "" Then
Label_First_Name.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Address.Value = "" Then
Label_Address.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Place.Value = "" Then
Label_Place.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_Country.Value = "" Then
Label_Country.ForeColor = RGB(255, 0, 0)
Else
'如果填写了表格,则值将粘贴到工作表中
Dim row_number As Integer, salutation As String
'选择上诉
For Each salutation_button In Frame_Salutation.Controls
If salutation_button.Value Then
salutation = salutation_button.Caption
End If
Next
'row_number = 列中最后一个非空单元格的行号 +1
row_number = Range("A65536").End(xlUp).Row + 1
'将值插入工作表
Cells(row_number, 1) = salutation
Cells(row_number, 2) = TextBox_Last_Name.Value
Cells(row_number, 3) = TextBox_First_Name.Value
Cells(row_number, 4) = TextBox_Address.Value
Cells(row_number, 5) = TextBox_Place.Value
Cells(row_number, 6) = ComboBox_Country.Value
'插入数据后,我们返回初始值
OptionButton1.Value = True
TextBox_Last_Name.Value = ""
TextBox_First_Name.Value = ""
TextBox_Address.Value = ""
TextBox_Place.Value = ""
ComboBox_Country.ListIndex = -1
End If
End Sub
以下是可供下载的 Excel 文件: controls_exercise2.xls