改变属性
通过视图调出工具箱
选中命令
和窗体一样,自己设置属性
就可以写这段代码,在窗体中
Private Sub CommandButton1_Click()
Dim r As Range, i As Long
Set r = Worksheets(1).UsedRange
'worksheets(1)不能省略
i = r.Row + r.Rows.Count
Cells(i, 2) = txtName.Text
Cells(i, 3) = txtAge.Text
'i表示当前表格最后一行数据的下一行
Unload fminput
'每次运行完都销毁掉窗口以及存放内存的数据
End Sub
Sub formshow()
fminput.Show
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If (Target.Column = 2 Or Target.Column = 3) And Cells(Target.Row, Target.Column) <> "" Then
fminput.txtName.Text = Cells(Target.Row, 2)
fminput.txtAge.Text = Cells(Target.Row, 3)
fminput.Show
End If
End Sub
在文本框中间实现换行,需要把属性中的multiLine改为TRUE
并且在文本框中换行,用 Ctrl+Enter,也可以通过修改enterkeybehavior来修改,使之能够用
enter实现换行
scrollbars可以用于生成滚动条
也可以用list语句进行修改,不过已经指定了rowsource,就不能在使用list设定其内容
可以设定属性为空串,也可以写代码置为空串
Private Sub UserForm_Initialize()
Dim a(4) As String
a(0) = "光"
a(1) = "空气"
a(2) = "土地"
a(3) = "水"
a(4) = "日月星辰"
ListBox1.RowSource = ""
'把rowsource置为空串
ListBox1.List = a
'把数组的值赋予文本框
End Sub
Private Sub CommandButton1_Click()
ListBox1.AddItem "钱"
'不要加等号
End Sub
Private Sub CommandButton2_Click()
ListBox1.RemoveItem 1
'删除的后面参数用序号,从0开始
End Sub
Private Sub CommandButton3_Click()
If Not IsNull(ListBox1.Value) Then
'防止未选择时报错
MsgBox ListBox1.Value
End If
End Sub
Sub 显示列表框()
UserForm2.Show
End Sub
Private Sub CommandButton1_Click()
Dim i As Long
If ListBox1.ListIndex > -1 Then
i = ListBox1.ListIndex + 2
'listindex返回的是对应的序列顺序,通过+2得到对应cells
txtscore1.Text = Cells(i, 3)
‘给文本框赋值
txtscore2.Text = Cells(i, 4)
txtscore3.Text = Cells(i, 5)
End If
End Sub
切换到另一个字段,右边清零
Private Sub ListBox1_Click()
Dim i As Long
If ListBox1.ListIndex > -1 Then
i = ListBox1.ListIndex + 2
txtscore1.Text = ""
txtscore2.Text = ""
txtscore3.Text = ""
End If
End Sub
也可以不使用控件,当鼠标单击在字段上,实现自动跳转
Private Sub ListBox1_Change()
Dim i As Long
If ListBox1.ListIndex > -1 Then
i = ListBox1.ListIndex + 2
txtscore1.Text = Cells(i, 3)
txtscore2.Text = Cells(i, 4)
txtscore3.Text = Cells(i, 5)
End If
End Sub
做法同列表框
也是把rowresource设置为需要的范围
Private Sub ComboBox1_Change()
Dim i As Long
If ComboBox1.ListIndex > -1 Then
i = ComboBox1.ListIndex + 2
txtscore1.Text = Cells(i, 3)
txtscore2.Text = Cells(i, 4)
txtscore3.Text = Cells(i, 5)
End If
End Sub
通过两个frame隔开两组单选,否则一个窗体上所有的单选都会视为一组。
另外,由于VBA中的名称不能以字母打头,但caption可以,所以有时候这两个不能设置为一样的字段
Private Sub CommandButton1_Click()
Dim gender As String, interest As String
If 男性.Value Then
gender = 男性.Caption
ElseIf 女性.Value Then
gender = 女性.Caption
Else
gender = 未注明.Caption
End If
Cells(4, 2) = gender
If 篮球.Value Then interest = interest & 篮球.Caption
If 足球.Value Then interest = interest & 足球.Caption
If 网球.Value Then interest = interest & 网球.Caption
'处理复选框尽量不用elseif,这里因为可能有多个结果
'所以写法上用连字符
Cells(4, 4) = interest
End Sub
选中图像,在picture里面直接选出URL
工具箱->右键可以设置出更多控件
Private Sub CommandButton1_Click()
TextBox1.Enabled = True
End Sub
Private Sub CommandButton2_Click()
TextBox1.Enabled = False
End Sub
Private Sub CommandButton3_Click()
TextBox1.Visible = True
End Sub
Private Sub CommandButton4_Click()
TextBox1.Visible = False
End Sub