上节我们讲到了控件的增加和基础代码,今天继续讲解控件的触发事件。
这一节的视频内容讲解,讲的不好,请不要见笑。
代码就不解释了,自己看视频吧!想获取本章的源代码请和视频资料请加微信:18622923987,或者直接淘宝扫描下面二维码:
代码如下:
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) ’文字输入框的代码
Dim i As Integer
Dim Language As Boolean
Dim myStr As String
Me.ListBox1.Clear
With Me.TextBox1
For i = 1 To Len(.Value)
If Asc(Mid$(.Value, i, 1)) > 255 Or Asc(Mid$(.Value, i, 1)) < 0="">
Language = True
myStr = myStr & Mid$(.Value, i, 1)
Else
myStr = myStr & LCase(Mid$(.Value, i, 1))
End If
Next
End With
With Sheets('商品')
For i1 = 2 To .Range('B65536').End(xlUp).Row
If Language = True Then
If Left(.Cells(i1, 3).Value, Len(myStr)) = myStr Then
Me.ListBox1.AddItem .Cells(i1, 2).Value
End If
Else
If Left(.Cells(i1, 3).Value, Len(myStr)) = myStr Then
Me.ListBox1.AddItem .Cells(i1, 2).Value
End If
End If
Next
End With
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range) ’表格选择改变代码
If Target.Column <> 1 And Cells(Range('a999999').End(xlUp).Row, 1) = '' Then
Cells(Range('a999999').End(xlUp).Row + 1, 1).Select
Else
Dim i As Integer
If Target.Count = 1 And ActiveCell = '' Then
If Target.Column = 1 And Target.Row > 1 Then
With Me.sj
.Visible = True
.Top = Target.Top
.Left = Target.Left
.Width = Target.Width
.Height = Target.Height
End With
End If
End If
If Target.Count = 1 And ActiveCell = '' Then
If Target.Column = 3 And Target.Row > 1 Then
If Cells(Target.Row, Target.Column - 2) <> '' Then
With Me.TextBox1
.Visible = True
.Visible = True
.Top = Target.Top
.Left = Target.Left
.Width = Target.Width
.Height = Target.Height
End With
With Me.ListBox1
.Visible = True
.Top = Target.Top
.Left = Target.Offset(0, 1).Left
.Width = Target.Width * 3
End With
Else
Exit Sub
End If
End If
End If
End If
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
ActiveCell.Value = ListBox1.Value
Me.ListBox1.Clear
Me.TextBox1 = ''
Me.ListBox1.Visible = False
Me.TextBox1.Visible = False
Me.ListBox2.Visible = True
Me.ListBox2.Clear
For i11 = 2 To Sheets('商品').Range('B65536').End(xlUp).Row
If Sheets('商品').Cells(i11, 2).Value = ActiveCell.Value Then
ListBox2.AddItem Sheets('商品').Cells(i11, 1).Value & ' ( ' & Sheets('商品').Cells(i11, 4).Value
End If
Next
ActiveCell.Offset(0, -1).Select
With Me.ListBox2
.Top = ActiveCell.Top
.Left = ActiveCell.Offset(0, 1).Left
End With
End Sub
联系客服