前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ExcelVBA联想输入学习

ExcelVBA联想输入学习

作者头像
哆哆Excel
发布2022-10-25 14:15:40
8060
发布2022-10-25 14:15:40
举报
文章被收录于专栏:哆哆Excel

ExcelVBA联想输入学习

【问题】我想在明细表的“名称”列中输入内容时“联想输入”,数据来源于后面的“单价”表中。输入框输入时列表框引用相应的名称与单价,点击后在明细表的“名称”列与“单价”列中自动输入单价表中的相应的内容

代码如下:

代码语言:javascript
复制
Private Sub yhdinput_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim s, arr
    s = Me.yhdinput.Value
    Me.yhdListBox.Visible = 1
    Me.yhdListBox.Clear
    arr = Sheets("单价").[a1].CurrentRegion
    For i = 2 To UBound(arr)
        If InStr(arr(i, 1), s) Then Me.yhdListBox.AddItem arr(i, 1) & "|" & arr(i, 2)
    Next i
    Exit Sub
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Rows.Count > 1 Or Target.Columns.Count > 1 Or Target.Areas.Count > 1 Then GoTo 100: Exit Sub
If Target.Column <> 5 Or Target.Row < 3 Then GoTo 100: Exit Sub
        With Me.yhdinput
            .Visible = True
            .Top = ActiveCell.Top
            .Left = ActiveCell.Left
            .Width = ActiveCell.Width
            .Height = ActiveCell.Height
            .Value = ""
            .Activate
        End With
        With Me.yhdListBox
            .Visible = True
            .Top = ActiveCell(1, 2).Top
            .Left = ActiveCell(1, 2).Left
            .Width = ActiveCell.Width * 1.5
            .Height = ActiveCell.Height * 8
            .Clear
        End With
        Exit Sub

100:
        Me.yhdinput = ""
        Me.yhdListBox.Clear
        Me.yhdinput.Visible = False
        Me.yhdListBox.Visible = False
End Sub

Private Sub yhdListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Dim t_arr
    t_arr = Split(Me.yhdListBox.Value, "|")
    ActiveCell.Value = t_arr(0)
    ActiveCell.Offset(0, 3).Value = t_arr(1)
    Me.yhdinput.Value = ""
    Me.yhdListBox.Clear
    Me.yhdinput.Visible = False
    Me.yhdListBox.Visible = False
'    ActiveCell.Offset(1, 0).Select
End Sub

Private Sub yhdListBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
 Dim t_arr
If KeyCode = 13 Then
    t_arr = Split(Me.yhdListBox.Value, "|")
    ActiveCell.Value = t_arr(0)
    ActiveCell.Offset(0, 3).Value = t_arr(1)
    Me.yhdinput.Value = ""
    Me.yhdListBox.Clear
    Me.yhdinput.Visible = False
    Me.yhdListBox.Visible = False

End If
End Sub

【坑】开始我是用这代码,结果出现如果选中最左边一格(全选)会出现溢出

If Target.Count > 1 Then Exit Sub

用下面的代码就可以解决了

If Target.Rows.Count > 1 Or Target.Columns.Count > 1 Or Target.Areas.Count > 1 Then Exit Sub

=======本代码个人学习之用======

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 哆哆Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档