|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Private Sub TextBox1_Change()
- If x = 1 Then '
- ActiveCell = TextBox1.Value
- Me.ListBox1.Clear
- On Error Resume Next
- <font color="#ff0000"> For i = LBound(frr) To UBound(frr)</font> '<font color="#ff0000">会出现错误这段会偶尔报错下标越界</font>
- If InStr(frr(i), Me.TextBox1.Value) > 0 Then
- Me.ListBox1.AddItem frr(i)
- End If
- Next
- If TextBox1 <> "" And ListBox1.ListCount > 0 Then ListBox1.ListIndex = 0
- End If
- '---------------------------------------------------------------------------
- If x = 2 Then '工程名称
- ActiveCell = TextBox1.Value
- Me.ListBox1.Clear
- For i = LBound(frr) To UBound(frr)
- If InStr(frr(i), Me.TextBox1.Value) > 0 Then
- Me.ListBox1.AddItem frr(i)
- End If
- Next
- If TextBox1 <> "" And ListBox1.ListCount > 0 Then ListBox1.ListIndex = 0
-
- End If
- End Sub
- Private Sub TextBox1_GotFocus()
- Me.ListBox1.Clear
- If x = 1 Then '公司名称
- If IsEmpty(drr) Then
- With Worksheets("基础信息表")
- drr = .Range("a2:h" & .Range("A65536").End(xlUp).Row)
- End With
- End If
- Dim dic
- Set dic = CreateObject("scripting.dictionary") '建立字典
- For i = LBound(drr) To UBound(drr)
- dic(drr(i, 2)) = 1
- Next
- Me.ListBox1.List = dic.keys '获得对应规格型号
- frr = dic.keys
- End If
- If x = 2 Then '工程名称
- Me.ListBox1.Clear
- If IsEmpty(drr) Then
- With Worksheets("基础信息表")
- drr = .Range("a2:h" & .Range("A65536").End(xlUp).Row)
- End With
- End If
- Dim gd As String
- gd = ActiveCell.Offset(-1, 0).Value
- If gd = "" Then Exit Sub
- Dim dic2
- Set dic2 = CreateObject("scripting.dictionary") '建立字典
- For i = LBound(drr) To UBound(drr)
- If drr(i, 2) = gd Then
- dic2(drr(i, 3)) = 1
- End If
- Next i
- grr = dic2.keys '
- Me.ListBox1.List = dic2.keys
- frr = dic2.keys
- Dim dic1
- Set dic1 = CreateObject("scripting.dictionary") '建立字典
- For i = LBound(grr) To UBound(grr)
- If InStr(grr(i), Me.TextBox1.Value) > 0 Then
- dic1(grr(i)) = 1
- End If
- Next i
- Me.ListBox1.List = dic1.keys '获得对应规格型号
- End If
- End Sub
- Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
- Dim i As Integer
- Select Case KeyCode
- Case vbKeyDown
- i = ListBox1.ListIndex + 1
- If i < ListBox1.ListCount Then ListBox1.ListIndex = i Else ListBox1.ListIndex = 0
- Case vbKeyUp
- i = ListBox1.ListIndex - 1
- If i > -1 Then ListBox1.ListIndex = i Else ListBox1.ListIndex = ListBox1.ListCount - 1
- Case vbKeyReturn
- If ListBox1.ListIndex = -1 Then
- ActiveCell.Offset(1, 0).Activate
- Else
- ActiveCell = ListBox1.List(ListBox1.ListIndex)
- ActiveCell.Offset(0, 1).Activate
- End If
- End Select
- End Sub
- Private Sub Worksheet_SelectionChange(ByVal Target As Range) '鼠标选定触发
- drr = Sheet1.Range("a2:h" & Sheet1.Range("A65536").End(xlUp).Row)
- If Target.Count > 1 Then Exit Sub
- With TextBox1
- If (Target.Column = 2 And Target.Row = 1) Or (Target.Column = 2 And Target.Row = 2) Then '第 列第 行
- x = Target.Row
- .Top = Target.Top
- .Width = Target.Width
- .Left = Target.Left
- .Height = Target.Height
- .Value = Target.Value
- .Visible = True
- .Activate
- With ListBox1
- .Top = TextBox1.Top + TextBox1.Height
- .Left = TextBox1.Left
- .Width = TextBox1.Width
- .Visible = True
- .ListIndex = -1
- End With
- Else
- .Visible = False
- ListBox1.Visible = False
- End If
- End With
- End Sub
复制代码 此段代码为了实现二级模糊匹配输入公司名称和对应的工程名称,红色部分偶尔会出现下标越界的错误提示,猜测是因为frr没有取到值,尝试使用 IsEmpty(frr)判断为空从新获取数据,发现frr为空时也不认定为空,求高手解惑,谢谢!
|
|