Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1259|回复: 0

求修改代码

[复制链接]
发表于 2022-11-26 21:29 | 显示全部楼层 |阅读模式
3学分
本帖最后由 18839208898 于 2022-11-28 21:02 编辑

模糊查找出来的内容用鼠标点击选择可以正常写入科目代码和科目列内,在科目代码列内直接输入代码模糊查找出来的科目右键移动选择后,按回车无效,不能自动写入科目列,麻烦热心的高手们帮忙看看吧,
Dim ARR
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    R = Selection.Row
        Cells(R, "B") = ListBox1.List(ListBox1.ListIndex, 0)
        Cells(R, "I") = ListBox1.List(ListBox1.ListIndex, 1)
    Me.ListBox1.Clear
    Me.TextBox1 = ""
    Me.ListBox1.Visible = False
    Me.TextBox1.Visible = False
    ActiveCell.Offset(1, 0).Select
End Sub
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim i As Integer
    Dim Language As Boolean, arr1 As Variant, arr2 As Variant, arr3 As Variant, arr4 As Variant
    Dim myStr As String, str_B As String
    With Me.ListBox1
        .Clear
        ARR = Sheets("科目").Range("A2:B" & Sheets("科目").Range("A65536").End(3).Row)
        For i = 1 To UBound(ARR)
            If InStr(ARR(i, 1), TextBox1.Text) Then
                R = ListBox1.ListCount
                .AddItem
                .List(R, 0) = ARR(i, 1)
                .List(R, 1) = ARR(i, 2)
            End If
        Next
    End With
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim i As Integer, ARR
    If Target.Count = 1 Then
        If Target.Column = 2 And Target.Row > 1 Then
            ARR = Sheets("科目").Range("A2:B" & Sheets("科目").Range("A65536").End(3).Row)
            With Me.TextBox1
                .Visible = True
                .Top = Target.Top
                .Left = Target.Left
                .Width = Target.Width
                .Height = Target.Height
                .Activate
            End With
            With Me.ListBox1
                .Visible = True
                .Top = Target.Top
                .Left = Target.Left + Target.Width
                .Width = Target.Width * 4
                .Height = Target.Height * 5
                .ColumnCount = 2
                .ColumnWidths = "60,120"
                .Column() = Application.Transpose(Array("科目代码", "科目名称"))
                For i = 1 To UBound(ARR)
                    .AddItem
                    .List(i, 0) = ARR(i, 1)
                    .List(i, 1) = ARR(i, 2)
                Next
            End With
        Else
            Me.ListBox1.Clear
            Me.TextBox1 = ""
            Me.ListBox1.Visible = False
            Me.TextBox1.Visible = False
        End If
    End If
End Sub
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    With TextBox1
        Select Case KeyCode
        Case 27    'Esc
            TextBox1.Visible = False
            ListBox1.Visible = False
            Selection.Select
        Case 38    '向上
            ActiveCell.Offset(-1, 0).Select
        Case 40    '向下
            ActiveCell.Offset(1, 0).Select
        End Select
    End With
    With Me.ListBox1
        If KeyCode = 39 And .ListCount > 0 Then         '键盘 右 键
            If .ListCount > 1 Then .ListIndex = 1 Else .ListIndex = -1
            .Activate
        End If
    End With
End Sub
Private Sub ListBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
AAA = KeyCode

    If KeyCode = 13 And Me.ListBox1.ListIndex > 0 Then     '键盘 Enter 键
        R = Selection.Row
        Cells(R, "B") = ListBox1.List(ListBox1.ListIndex, 0)
        Cells(R, "I") = ListBox1.List(ListBox1.ListIndex, 1)
    Me.ListBox1.Clear
    Me.TextBox1 = ""
    Me.ListBox1.Visible = False
    Me.TextBox1.Visible = False
    ActiveCell.Select
    End If


End Sub

微信图片_20221126211542.png

模糊输入.rar

48.16 KB, 下载次数: 5

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-3 20:47 , Processed in 0.253620 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表