【问题标题】:VBA Excel dropdown list autocomplete crash issueVBA Excel下拉列表自动完成崩溃问题
【发布时间】:2020-07-28 19:12:52
【问题描述】:

我正在尝试创建一个允许在输入数据验证单元格时自动完成的 VBA。我从以下问题What VBA event allows to capture click value of ActiveX combobox?获得了代码。

问题是,当我使用该问题中的以下代码并让它超出我的名称范围(它正在搜索的 200 个段落的列表)时,它会立即让我从 excel 中崩溃,我不知道为什么.代码是否存在问题,或者在 200 个单独的单元格中搜索 200 个段落,而 VBA 不可行?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim xCombox As OLEObject
    Dim xStr As String
    Dim xWs As Worksheet
    Dim xArr
    Set xWs = Application.ActiveSheet
    On Error Resume Next
    Set xCombox = xWs.OLEObjects("TempCombo")
    With xCombox
        .ListFillRange = ""
        .LinkedCell = ""
        .Visible = False
    End With
    If Target.Validation.Type = 3 Then
        Target.Validation.InCellDropdown = False
        'Cancel = True
        xStr = Target.Validation.Formula1
        xStr = Right(xStr, Len(xStr) - 1)
        If xStr = "" Then Exit Sub
        With xCombox
            .Visible = True
            .Left = Target.Left
            .Top = Target.Top
            .Width = Target.Width + 5
            .Height = Target.Height + 5
            .ListFillRange = xStr
            If .ListFillRange = "" Then
                xArr = Split(xStr, Application.International(xlListSeparator))
                Me.TempCombo.List = xArr
            End If
            .LinkedCell = Target.Address
        End With
        xCombox.Activate
        Me.TempCombo.DropDown
    End If
End Sub

Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case 9 'tab
            Application.ActiveCell.Offset(0, 1).Activate
        Case 13 'enter
            Application.ActiveCell.Offset(1, 0).Activate
    End Select
End Sub

Private Sub TempCombo_Change()
If Me.TempCombo = "" Then Exit Sub
ActiveSheet.OLEObjects(1).ListFillRange = ""
ActiveSheet.OLEObjects("TempCombo").Object.Clear
ThisWorkbook.ActiveSheet.OLEObjects("TempCombo").Activate

With Me.TempCombo
    If Not .Visible Then Exit Sub
    .Visible = False 'to refresh the drop down
    .Visible = True
    .Activate

'Dump the range into a 2D array
        Dim Arr2D As Variant
        Arr2D = [QoE].Value

'Declare and resize the 1D array
        Dim Arr1D As Variant
        ReDim Arr1D(1 To UBound(Arr2D, 1))

'Convert 2D to 1D
        Dim i As Integer
        For i = 1 To UBound(Arr2D, 1)
            Arr1D(i) = Arr2D(i, 1)
        Next

    Dim itm As Variant 'itm is for iterate purpose
    Dim ShortItemList() As Variant 'ShortItemList() is a variable which stores only filtered items
    i = -1
    For Each itm In Arr1D
        If InStr(1, itm, .Value, vbTextCompare) > 0 Or .Value = "" Then
            Debug.Print itm
             i = i + 1
             ReDim Preserve ShortItemList(i)
             ShortItemList(i) = itm
        End If
    Next itm
    .DropDown
End With

On Error Resume Next 'if we filter too much, there will be no items on ShortItemList
ThisWorkbook.ActiveSheet.OLEObjects("TempCombo").Object.List = ShortItemList

End Sub

【问题讨论】:

  • 删除 On Error Resume Next 并使用它给您的错误编辑您的问题。
  • 我删除了它,但当我开始输入数据验证单元格时,我仍然只是被踢出局。没有错误显示,它只是完全关闭了 excel @Mech
  • 进入你的vba代码,按F8直到你找到它崩溃的那一行。
  • 以下行都显示为代码“Private Sub TempCombo_Change() If Me.TempCombo = "" Then Exit Sub"中的中断
  • 是崩溃了还是退出了?

标签: excel vba autocomplete crash


【解决方案1】:

您不使用 .DropDown。当 ComboBox 隐藏或删除时,它会导致 Excel 崩溃

【讨论】:

猜你喜欢
  • 2020-09-08
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多