我发现窗体和控件的很多属性,既可以在VBE的属性窗口设置,也可以在VBA代码里设置,我总结了下差别如下
通用属性
有一定特殊性的属性的
Private Sub UserForm_Initialize()
Me.StartUpPosition = 0
Me.Top = 0
Me.Left = 0
end sub
Private Sub UserForm_Initialize()
'窗体自身初始化显示
窗体3.Caption = "本窗体对应的工作表range是 " & ActiveWorkbook.Name & "的sheet: " & Sheet3.Name
'label的初始化
Label1.BackStyle = 0
Label1.Caption = "请输入您想查找的新海诚电影名"
Label1.ForeColor = RGB(255, 255, 255)
Label2.Caption = Worksheets("窗体3对应").Range("a1")
Label3.Caption = Worksheets("窗体3对应").Range("C1")
'listbox的初始化
'ListBox1.RowSource = "窗体3对应!A1:A10" '这样设置会导致additem removeitem报错
'ListBox2.RowSource = "窗体3对应!c1:c10"
For i = 2 To Worksheets("窗体3对应").Range("a65536").End(xlUp).Row '这样写会很卡Range("a:a").Rows.Count
ListBox1.AddItem Range("a" & i)
Next
For i = 2 To Worksheets("窗体3对应").Range("c65536").End(xlUp).Row
ListBox2.AddItem Range("c" & i)
Next
'Frame1.BackStyle = 0
'farme1 难道不能设置背景透明?
End Sub
Private Sub WindowsMediaPlayer1_Click(ByVal nButton As Integer, ByVal nShiftState As Integer, ByVal fX As Long, ByVal fY As Long)
WindowsMediaPlayer1.URL = "C:\Users\Administrator\Desktop\rain.mp3"
WindowsMediaPlayer1.Controls.Play
窗体3.Picture = LoadPicture("C:\Users\Administrator\Desktop\timg2.jpg")
End Sub
Private Sub Label1_Click()
'label的很多通用属性
Label1.Visible = True
Label1.enanbled = True
'label1的文本属性
debug.print label1.caption
'label1没有如下这些属性
'Label1.Value = 1
'Label1.Text = 111
'Label1.Default = 222
End Sub
Private Sub Label4_Click()
For i = 1 To 26 '我希望是循环播放字幕,这样不行, 还想实现字幕左右移动。。。
Label4.Caption = Range("f" & Application.Match(i, Range("e1:e100"), 0)).Value
Debug.Print Label4.Caption
' Application.Wait Now + TimeValue("00:00:03") '整个程序会卡停
Sleep 3000
DoEvents
Next
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
sleep 2000
Private Sub CommandButton3_Click()
'增加记录按钮,输入
Range("B15") = TextBox1.Text
Range("B16") = TextBox1.Value
Range("B17") = TextBox1
Debug.Print TypeName(Range("B15"))
Debug.Print TypeName(Range("B16"))
Debug.Print TypeName(Range("B17"))
Debug.Print TypeName(Range("B15").Value)
Debug.Print TypeName(Range("B16").Value)
Debug.Print TypeName(Range("B17").Value)
End Sub
Private Sub UserForm_Initialize()
'窗体自身初始化显示
窗体3.Caption = "本窗体对应的工作表range是 " & ActiveWorkbook.Name & "的sheet: " & Sheet3.Name
'label的初始化
Label1.BackStyle = 0
Label1.Caption = "请输入您想查找的新海诚电影名"
Label1.ForeColor = RGB(255, 255, 255)
Label2.Caption = Worksheets("窗体3对应").Range("a1")
Label3.Caption = Worksheets("窗体3对应").Range("C1")
'listbox的初始化
'ListBox1.RowSource = "窗体3对应!A1:A10" '这样设置会导致additem removeitem报错
'ListBox2.RowSource = "窗体3对应!c1:c10"
For i = 2 To Worksheets("窗体3对应").Range("a65536").End(xlUp).Row '这样写会很卡Range("a:a").Rows.Count
ListBox1.AddItem Range("a" & i)
Next
For i = 2 To Worksheets("窗体3对应").Range("c65536").End(xlUp).Row
ListBox2.AddItem Range("c" & i)
Next
'Frame1.BackStyle = 0
'farme1 难道不能设置背景透明?
TextBox1.MultiLine = True '可以换行
TextBox1.MaxLength = 20 '最大字数
End Sub
TextBox1.MultiLine = True '可以换行
TextBox1.MaxLength = 20 '最大字数
'TextBox1.PasswordChar = "#"
Private Sub CommandButton3_Click()
'增加记录按钮,输入
Dim a As Object
b = TextBox1.Value
Set a = Range("a1:" & "a" & Range("a65536").End(xlUp).Row).Find(b)
If a Is Nothing Then
Range("a" & 1 + Range("a65536").End(xlUp).Row).Value = b
Else
MsgBox "您输入的内容重复了"
'goto '这里加goto语句意义不大,用户用inputbox输入,又不是inputbox
End If
TextBox1.Value = ""
End Sub
Private Sub CommandButton4_Click()
'查找按钮
Dim a As Range
Dim b
b = TextBox1.Value
Set a = Range("a1:a" & Range("a65536").End(xlUp).Row).Find(b)
If a Is Nothing Then
MsgBox "你要查找的内容并不存在"
Else
MsgBox "你要查找的内容,已经存在"
End If
End Sub
Private Sub CommandButton5_Click()
'删除按钮
Dim a As Range
Dim b
b = TextBox1.Value
Set a = Range("a1:a" & Range("a65536").End(xlUp).Row).Find(b)
If a Is Nothing Then
MsgBox "你要删除的内容并不存在"
Else
c = MsgBox("您确定要删除吗?", vbOKCancel) '最好二次确认后确定,查返回值
If c = vbOK Then
a = ""
MsgBox "你要删除的内容,已经删除"
End If
End If
TextBox1.Value = ""
End Sub
Private Sub UserForm_Initialize()
'窗体自身初始化显示
窗体3.Caption = "本窗体对应的工作表range是 " & ActiveWorkbook.Name & "的sheet: " & Sheet3.Name
'label的初始化
Label1.BackStyle = 0
Label1.Caption = "请输入您想查找的新海诚电影名"
Label1.ForeColor = RGB(255, 255, 255)
Label2.Caption = Worksheets("窗体3对应").Range("a1")
Label3.Caption = Worksheets("窗体3对应").Range("C1")
'listbox的初始化
'ListBox1.RowSource = "窗体3对应!A1:A10" '这样设置会导致additem removeitem报错
'ListBox2.RowSource = "窗体3对应!c1:c10"
For i = 2 To Worksheets("窗体3对应").Range("a65536").End(xlUp).Row '这样写会很卡Range("a:a").Rows.Count
ListBox1.AddItem Range("a" & i)
Next
For i = 2 To Worksheets("窗体3对应").Range("c65536").End(xlUp).Row
ListBox2.AddItem Range("c" & i)
Next
End Sub
Private Sub CommandButton1_Click()
'向右箭头
ListBox2.AddItem ListBox1.Text
ListBox1.RemoveItem ListBox1.ListIndex
End Sub
Private Sub CommandButton2_Click()
'向左箭头
ListBox1.AddItem ListBox2.Text
ListBox2.RemoveItem ListBox2.ListIndex
End Sub
Private Sub UserForm_Initialize()
ListBox1.RowSource = "sheet3!a2:c10"
ListBox1.ColumnCount = 3
ListBox1.ColumnWidths = "50;80;50" '实测用两种分隔都可以 "50,100,50"
ListBox1.ColumnHeads = True ’设置了heads好像会自动往上找1行
End Sub
方法 combobox1.dropdown '设置在鼠标移动过去的时候,自动打开下拉框
Private Sub ComboBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
ComboBox1.DropDown
End Sub
Private Sub UserForm_Initialize()
'combobox1的初始化
ComboBox1.RowSource = "窗体3对应!c1:c110"
ComboBox1.ControlSource = "窗体3对应!d1"
ComboBox1.MatchRequired = True '必须是rowsource内容啊
ComboBox1.MatchEntry = 1 '好像无论设置0,1 都不限制输入内容??
End Sub
Private Sub OptionButton1_Change()
Debug.Print OptionButton1.Caption
End Sub
Private Sub OptionButton2_Change()
Debug.Print OptionButton2.Caption
End Sub
Private Sub OptionButton3_Change()
Debug.Print OptionButton3.Caption
End Sub
Private Sub OptionButton1_Click()
Debug.Print OptionButton1.Caption
End Sub
Private Sub OptionButton2_Click()
Debug.Print OptionButton2.Caption
End Sub
Private Sub OptionButton3_Click()
Debug.Print OptionButton3.Caption
End Sub
Private Sub UserForm_Initialize()
ListBox3.AddItem "timg1"
ListBox3.AddItem "timg2"
End Sub
Private Sub ListBox3_Change()
Image1.Picture = LoadPicture(ThisWorkbook.Path & "\" & ListBox3.Value & ".jpg")
End Sub
Private Sub WindowsMediaPlayer1_Click(ByVal nButton As Integer, ByVal nShiftState As Integer, ByVal fX As Long, ByVal fY As Long)
WindowsMediaPlayer1.URL = "C:\Users\Administrator\Desktop\rain.mp3"
WindowsMediaPlayer1.Controls.Play
End Sub
Private Sub SpinButton1_Change()
TextBox2.Value = SpinButton1.Value
End Sub
Private Sub SpinButton1_SpinDown()
TextBox3.Value = TextBox3.Value - 1
End Sub
Private Sub SpinButton1_SpinUp()
TextBox3.Value = TextBox3.Value + 1
End Sub
Private Sub UserForm_Initialize()
'spinbutton相关的初始化
SpinButton1.Max = 10
SpinButton1.Min = 1
TextBox2.Value = 1
TextBox3.Value = 1
'下面这部分代码,和本部分内容无关----------------------------------------
'窗体自身初始化显示
窗体3.Caption = "本窗体对应的工作表range是 " & ActiveWorkbook.Name & "的sheet: " & Sheet3.Name
'label的初始化
Label1.BackStyle = 0
Label1.Caption = "请输入您想查找的新海诚电影名"
Label1.ForeColor = RGB(0, 0, 0)
Label2.Caption = Worksheets("窗体3对应").Range("a1")
Label3.Caption = Worksheets("窗体3对应").Range("C1")
'listbox的初始化
'ListBox1.RowSource = "窗体3对应!A1:A10" '这样设置会导致additem removeitem报错
'ListBox2.RowSource = "窗体3对应!c1:c10"
For i = 2 To Worksheets("窗体3对应").Range("a65536").End(xlUp).Row '这样写会很卡Range("a:a").Rows.Count
ListBox1.AddItem Range("a" & i)
Next
For i = 2 To Worksheets("窗体3对应").Range("c65536").End(xlUp).Row
ListBox2.AddItem Range("c" & i)
Next
'Frame1.BackStyle = 0
'farme1 难道不能设置背景透明?
TextBox1.MultiLine = True '可以换行
TextBox1.MaxLength = 20 '最大字数
'TextBox1.PasswordChar = "#"
End Sub