Skip to content

窗体与控件

251字小于1分钟

2024-07-07

窗体

Private Sub UserForm_Activate()
Application.Visible = False
End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Quit
End Sub

文本框控件

Private Sub CommandButton1_Click()

If UserForm1.TextBox1.Value = "张三" And UserForm1.TextBox2.Value = "123" Then
    Sheet2.Unprotect Password:="test"
    Sheet2.Visible = xlSheetVisible
    Me.Hide
ElseIf UserForm1.TextBox1.Value = "李四" And UserForm1.TextBox2.Value = "0000" Then
    Sheet3.Unprotect Password:="test"
    Sheet3.Visible = xlSheetVisible
    Me.Hide
End If

End Sub

组合框控件

'增加下拉框的选项
Sub test1()
UserForm1.ComboBox1.AddItem "苏州"
End Sub


'把单元格一列数据设置为下拉的数据源
Sub test2()
For i = 2 To 6
    UserForm1.ComboBox1.AddItem Sheet1.Range("a" & i)
Next
End Sub


'删除掉下拉框里的一项
Sub test3()
Call test2
UserForm1.ComboBox1.RemoveItem (1) '删除第二项
End Sub


'清空下拉框数据源
Sub test4()
Call test2
UserForm1.ComboBox1.Clear
End Sub

'计算下拉框里有多少个选项
Sub test5()
Call test2
MsgBox UserForm1.ComboBox1.ListCount
End Sub

'把下拉框数据源里某个值输出到单元格

Sub test6()
Call test2
Range("c2") = UserForm1.ComboBox1.List(1)
End Sub

'把下拉框里所有的选项输出到单元格区域
Sub test7()
Call test2
Range("d1:d5") = UserForm1.ComboBox1.List
End Sub