禁用vba代码单元格中的自动完成值

时间:2017-03-11 12:59:34

标签: excel vba excel-vba

我有一个vba代码,在输入下拉框时显示建议。除了此代码中的自动完成值之外,一切都很有效。我试图在excel中关闭自动完成值但问题仍然存在。例如:

数组中有3个值,我想选择第三个值:

Itoyori 100/200

Itoyori 300/500

Itoyori 400

每当我尝试输入一些首字母时,例如:ito,它自动建议Itoyori 100/200,因此我看不到最后两个值要选择。

这是测试文件: test file please download

以下是代码:

Private priArray
Private priIsFocus As Boolean

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 And Target.Row > 6 And Target.Column = 1 Then
    Dim e As Long
    Dim sh As Worksheet
    Set sh = Sheets("Sheet1")
    If Not IsArray(priArray) Then
        e = sh.Range("A" & Rows.Count).End(xlUp).Row
        priArray = sh.Range("A6:A" & e).Value2
    End If
    Call HienComboBox
Else
    Call AnComboBox
End If
End Sub

Private Sub ComboBox1_Change()
If priIsFocus Then Exit Sub
If ComboBox1.MatchFound Then
    ActiveCell.Value = ComboBox1.Text
    'ActiveCell.Offset(, 2).Value = ComboBox1.Column(2)
Else
    ActiveCell.Value = ""
    'ActiveCell.Offset(, 2).Value = ""
End If
End Sub

Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
On Error Resume Next
Select Case KeyCode
Case 16, 17, 37 To 40
Case 9
    ActiveCell.Offset(, 1).Activate
Case 13
    ActiveCell.Offset(1).Activate
Case Else
    If IsArray(priArray) Then
        Dim strValue As String
        strValue = LCase(ComboBox1.Text)
        ComboBox1.ListRows = 20
        If Trim(strValue) > "" Then
            Dim ArrFilter, GetRow()
            Dim c As Long, i As Long, n As Long, r As Long
            For r = 1 To UBound(priArray, 1)
                If LCase(priArray(r, 1)) Like "*" & strValue & "*" Then
                    n = n + 1
                    ReDim Preserve GetRow(1 To n)
                    GetRow(n) = r
                End If
            Next
            If n Then
                Dim u As Byte
                u = UBound(priArray, 2)
                ReDim ArrFilter(1 To n, 1 To u)
                For r = 1 To n
                    For c = 1 To u
                        ArrFilter(r, c) = priArray(GetRow(r), c)
                    Next
                Next
                ComboBox1.List = ArrFilter
            Else
                ComboBox1.Clear
                ComboBox1.ListRows = 0
            End If
            ComboBox1.DropDown
        Else
            If ComboBox1.ListCount <> UBound(priArray) Then
                ComboBox1.List = priArray
                ComboBox1.DropDown
            End If
        End If
    End If
End Select
End Sub

Private Sub HienComboBox()
priIsFocus = True
With ComboBox1
    .Visible = False
    .Visible = True
    .Left = ActiveCell.Left
    .Top = ActiveCell.Top
    .Width = ActiveCell.Width
    .ListWidth = ActiveCell.Resize(, 3).Width + 12
    .ColumnWidths = .Width - 4 & "," & ActiveCell.Offset(, 1).Width
    .Height = ActiveCell.Height
    .List = priArray
    .Text = ActiveCell.Value
    .Activate
    .SelStart = 0
    .SelLength = Len(.Text)
End With
priIsFocus = False
End Sub

Private Sub AnComboBox()
With ComboBox1
    If .Visible Then
        .Visible = False
    End If
End With
End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用向下键移动到下拉列表中的下一个项目。