【问题标题】:VBA Excel dropdown list autocomplete - problem with list autoupdateVBA Excel下拉列表自动完成 - 列表自动更新问题
【发布时间】:2020-09-08 07:03:10
【问题描述】:

下午好,

我想让我的列表自动更新工作,但我不知道该怎么做。

您可以在上面看到的列表是我的测试列表。右侧有组合框,我在其中设置了属性: 链接列为 R2 单元格范围为:A2:A6。

它工作正常,您可以在右侧下拉菜单中看到。单元格区域 A2:A6 中的记录已正确填充。

问题是,该列表将被开发,因为新数据将逐行出现。在这种情况下,我无法保持这个范围固定。

我尝试使用此处提供的代码对其进行管理:

dropdown list with autocomplete/ suggestion in excel vba

到目前为止,我的代码如下所示:

 Option Explicit

 Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim vType As XlDVType
  On Error GoTo EndLine
  vType = Target.Validation.Type

 Dim xCombox As OLEObject
 Dim xStr As String
 Dim xWs As Worksheet
 Dim Cancel As Boolean
 Set xWs = Application.ActiveSheet

  'On Error Resume Next
  Set xCombox = xWs.OLEObjects("Combobox1")
  With xCombox
   Dim OrigRange As Range: OrigRange = .ListFillRange
     If Not Application.Intersect(OrigRange, Target) Is Nothing Then
            .ListFillRange = .OrigRange.Resize(OrigRange.Cells(1).End(xlDown).Row - OrigRange.Row + 1)
    End If
    '.ListFillRange = .OrigRange.Resize(OrigRange.Cells(1).End(xlDown).Row - OrigRange.Row + 1)
    .LinkedCell = "=A"
    .Visible = False
   End With
   If vType = 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
        .LinkedCell = Target.Address
    End With
    xCombox.Activate
    Me.ComboBox1.DropDown
   End If
  EndLine:
 End Sub

代码与自动填充一起使用,很好。但是我不得不混合一些东西,因为我现在看不到任何反应。代码有效并且自动填充是正确的,但自动更新不起作用。我将新值放在下面,超出了属性中的固定单元格范围,它们不会出现在下拉列表中,您可以看到。

有什么办法让它运行吗?

更新:

最近解决问题的方法:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim vType As XlDVType
On Error GoTo EndLine
vType = Target.Validation.Type

Dim xCombox As OLEObject
Dim xStr As String
Dim xWs As Worksheet
Dim Cancel As Boolean
Set xWs = Application.ActiveSheet

'On Error Resume Next
Set xCombox = xWs.OLEObjects("Combobox1")
With xCombox
Dim OrigRange As Range: Set OrigRange = xWs.Range("A2:A" & xWs.Range("A" & xWs.Rows.Count).End(xlUp).Row)
If Not Application.Intersect(OrigRange, Target) Is Nothing Then
.ListFillRange = OrigRange.Address
End If
.LinkedCell = "R2" '??
.Visible = True 'Dont know why hide ??
 End With
If vType = 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
        .LinkedCell = Target.Address
    End With
    xCombox.Activate
    Me.ComboBox1.DropDown
 End If
 EndLine:
End Sub

删除 ListFillRange 属性后,组合框下拉列表完全空白。

【问题讨论】:

    标签: excel vba list combobox


    【解决方案1】:

    假设:代码的验证部分没有问题,因为它是一个带有公式的列表验证,很难想象。

    ComboBox ListFillRange 属性是一个字符串。

    所以

    Dim OrigRange As Range: OrigRange = .ListFillRange
    xCombox.ListFillRange = .OrigRange.Resize(OrigRange.Cells(1).End(xlDown).Row - OrigRange.Row + 1)
    

    不会工作

    Dim OrigRange As Range: Set OrigRange = Range(.ListFillRange)
    xCombox.ListFillRange = .OrigRange.Resize(OrigRange.Cells(1).End(xlDown).Row - OrigRange.Row + 1).Address
    

    会起作用

    但由于OrigRange 设置在相交条件之前,Target 范围将始终落在OrigRange 之外,即=Range(.ListFillRange),因此ListFillRange 将永远不会更新。用以下内容替换 With xCombox 块将有所帮助。试试:

    With xCombox
    Dim OrigRange As Range: Set OrigRange = xWs.Range("A2:A" & xWs.Range("A" & xWs.Rows.Count).End(xlUp).Row)
        If Not Application.Intersect(OrigRange, Target) Is Nothing Then
        .ListFillRange = OrigRange.Address
        End If
    .LinkedCell = "=A" '??
    .Visible = False 'Dont know why hide ??
    End With
    

    我想知道目标值是否受列表限制,那么您可以为组合框使用相同的列表。

    编辑 先试试他的代码。与验证相关的所有内容都被删除。如果这可行,您可以考虑验证,但我想知道为什么要验证!

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xCombox As OLEObject
    Dim xWs As Worksheet
    Set xWs = Application.ActiveSheet
    Dim OrigRange As Range
    Set OrigRange = xWs.Range("A2:A" & xWs.Range("A" & xWs.Rows.Count).End(xlUp).Row)
    Set xCombox = xWs.OLEObjects("Combobox1")
    With xCombox
    If Not Application.Intersect(OrigRange, Target) Is Nothing Then
    .ListFillRange = OrigRange.Address
    End If
        .LinkedCell = "R2" '??
        .Visible = True 'Dont know why hide ??
    End With
    End Sub
    

    【讨论】:

    • 不幸的是它不起作用。我打开组合框的编辑器模式并尝试从 A2:A 更改 ListFillRange,但每次都消失了,因为“A2:A”语句是错误的。当“ListFillRange”中没有任何内容时,我无法选择任何订单。查看我的代码根据您的情况更新。你能告诉我我哪里错了吗?
    • 您不必更改组合框属性中的 ListFillRange。这个过程会处理它。另外,Dim OrigRange As Range: Set OrigRange = xWs.Range("A2:A" & xWs.Range("A" & xWs.Rows.Count).End(xlUp).Row) 必须在同一行。它出现在新代码的两行中。
    • 我修复了代码,但也没有用。如图所示,我有空白组合框。另一个查询是:我应该从 Sheet1 运行此代码还是将其整个输入到 Combobox1 代码中?
    • 这是一个工作表事件,因此您必须从组合框所在的工作表中运行代码
    • 好的,我做到了,但它不起作用。你能跳过我的代码,看看有什么问题或遗漏吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    相关资源
    最近更新 更多