|
楼主 |
发表于 2015-3-15 11:35
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Private Sub CheckBox1_Click()
- If Me.CheckBox1.Value = False Then
- With Me.TextBox1
- .SelStart = 0
- .SelLength = Len(.Text)
- .SetFocus
- End With
- End If
- End Sub
- Private Sub ComboBox1_Change()
- If Me.CommandButton1.Caption = "修 改" Then Exit Sub
- With Me.ComboBox2
- If Me.ComboBox1.Text <> "" Then
- .Enabled = True
- .BackColor = &H80000005
- Else
- .Text = ""
- .Enabled = False
- .BackColor = &H8000000B
- End If
- End With
- End Sub
- Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
- Select Case Me.ComboBox1.Text
- Case "", "普通作物", "神秘作物"
- Case Else
- Me.ComboBox1.Text = ""
- Cancel = True
- End Select
- End Sub
- Private Sub ComboBox2_Change()
- With Me.ComboBox3
- If Me.ComboBox2.Text = "" Or Me.ComboBox2.Text = "其它" Then
- .Text = ""
- .Enabled = False
- .BackColor = &H8000000B
- Else
- .Enabled = True
- .BackColor = &H80000005
- If Me.CommandButton1.Caption = "修 改" Then .Text = br(1, 4)
- End If
- End With
- With Me.TextBox2
- If Me.ComboBox2.Text = "" And Me.CommandButton1.Caption = "新 增" Then
- .Text = ""
- .Enabled = False
- .BackColor = &H8000000B
- ElseIf Me.ComboBox2.Text = "其它" Then
- .Enabled = True
- .BackColor = &H80000005
- End If
- End With
- End Sub
- Private Sub ComboBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
- Select Case Me.ComboBox2.Text
- Case "", "简单系列", "一般系列", "挑战系列", "其它"
- Case Else
- Me.ComboBox2.Text = ""
- Cancel = True
- End Select
- End Sub
- Private Sub ComboBox3_Change()
- If Me.CommandButton1.Caption = "修 改" Then Exit Sub
- With Me.TextBox2
- If Me.ComboBox3.Text <> "" Then
- .Enabled = True
- .BackColor = &H80000005
- Else
- .Text = ""
- .Enabled = False
- .BackColor = &H8000000B
- End If
- End With
- End Sub
- Private Sub ComboBox3_Enter()
- Str = d(Me.ComboBox1.Text & "," & Me.ComboBox2.Text)
- If InStr(Str, ":") > 0 Then
- Me.ComboBox3.List = Split(Str, ":")
- Else
- Me.ComboBox3.AddItem Str
- End If
- End Sub
- Private Sub CommandButton1_Click()
- br = Array(Me.TextBox1.Text, Me.ComboBox1.Text, Me.ComboBox2.Text, Me.ComboBox3.Text, Me.TextBox2.Text)
- If Me.CommandButton1.Caption = "修 改" Then
- If br(0) = rng.Value Then
- n = 0
- For i = 1 To 4
- If br(i) <> rng.Offset(0, i).Text Then Exit For
- n = n + 1
- Next i
- If n = 4 Then
- ws.Popup "作物『" & br(0) & "』数据无变化,无须保存", 1, "信息", 64
- Exit Sub
- End If
- Str = "作物『" & br(0) & "』数据已更新"
- Else
- Str = "作物『" & rng.Value & "』数据已修改为" & "〖" & br(0) & "』"
- End If
- rng.Resize(1, 5) = br
- ws.Popup Str, 1, "信息", 64
- Set rng = Nothing
- Me.CommandButton1.Caption = "新 增"
- Else
- Sheets("Data").Range("A65536").End(xlUp).Offset(1, 0).Resize(1, 5) = br
- ws.Popup "作物『" & br(0) & "』数据已新增", 1, "信息", 64
- End If
- ThisWorkbook.Sheets("Data").Columns("A:E").Sort Key1:=Range("E1"), Order1:=xlAscending, Header:=xlYes
- Call Renew '更新字典数据
- Me.TextBox1.Text = ""
- Me.TextBox1.SetFocus
- End Sub
- Private Sub CommandButton2_Click()
- r = ws.Popup("是否删除该作物数据?", 2, "询问", 4 + 32)
- If r = 6 Then
- Str = "作物『" & rng.Value & "』数据已删除"
- rng.EntireRow.Delete
- Set rng = Nothing
- ws.Popup Str, 1, "信息", 64
- Call Renew '更新字典数据
- Me.TextBox1.Text = ""
- Me.TextBox1.SetFocus
- End If
- End Sub
- Private Sub CommandButton3_Click()
- Me.Hide
- UserForm1.Show
- End Sub
- Private Sub TextBox1_Change()
- Me.TextBox1.Text = Trim(Me.TextBox1.Text)
- If Me.TextBox1.Text = "" Then
- Me.ComboBox1.Enabled = False
- Me.ComboBox1.BackColor = &H8000000B
- Me.CommandButton1.Enabled = False
- GoTo 0
- End If
- If Me.CheckBox1.Value = False Then Exit Sub
- If Me.TextBox1.Text <> "" Then
- Me.ComboBox1.BackColor = &H80000005
- Me.ComboBox1.Enabled = True
- Set rng = ThisWorkbook.Sheets("Data").Range("A:A").Find(Me.TextBox1.Text, , , 1)
- If Not rng Is Nothing Then
- br = rng.Resize(1, 5)
- For i = 1 To 3
- Me.Controls("Combobox" & i).Text = br(1, i + 1)
- Next i
- Me.TextBox2.Text = br(1, 5)
- Me.CheckBox1.Enabled = True
- Me.CommandButton1.Caption = "修 改"
- Me.CommandButton2.Visible = True
- Else
- GoTo 0
- End If
- Else
- 0:
- Me.CheckBox1.Value = True
- Me.CheckBox1.Enabled = False
- Me.CommandButton1.Caption = "新 增"
- Me.CommandButton2.Visible = False
- For i = 1 To 3
- Me.Controls("Combobox" & i).Text = ""
- Next i
- Me.TextBox2.Text = ""
- End If
- End Sub
- Private Sub TextBox2_Change()
- Str = Me.TextBox2.Text
- If Str = "" Then
- Me.CommandButton1.Enabled = False
- Else
- For i = 1 To Len(Str)
- temp = Mid(Str, i, 1)
- Select Case temp
- Case 0 To 9
- Case Else
- Me.TextBox2.Text = Replace(Str, temp, "")
- End Select
- Next i
- Me.TextBox2.Text = Application.Text(Me.TextBox2.Text, "0")
- Me.CommandButton1.Enabled = True
- End If
- End Sub
- Private Sub UserForm_Activate()
- Me.TextBox1.Text = ""
- Me.TextBox1.SetFocus
- End Sub
- Private Sub UserForm_Initialize()
- Me.CheckBox1.Value = True
- Me.CheckBox1.Enabled = False
- Me.ComboBox1.List = Array("普通作物", "神秘作物")
- Me.ComboBox2.List = Array("简单系列", "一般系列", "挑战系列", "其它")
- End Sub
- Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
- If CloseMode = 0 Then '判断是否用窗体关闭按键来关闭窗口
- Cancel = 1 '设置为除0以外的任意值,可以终止窗体的关闭,与closemode同时使用,可以达到让某种关闭方式失效
- Me.Hide
- UserForm1.Show
- End If
- End Sub
复制代码
|
|