使用可搜索的多个列表 Excel VBA

问题描述

我正在制作一个条目数据,我实现了一个互联网代码来制作一个能够在一系列单元格内进行搜索的组合框。如果在数据验证列表中引用的值是绝对范围(例如 ='Posibles valores'!$P$4:$P$103),则代码运行良好。但我不想更新引用,而只是在表上创建新闻行。我试过这些方法

  1. 创建一个命名范围,其中包含一个函数 =INDIRECT("Tubos_Tipo[Tubos-Tipos]"。对表进行引用。但在这种情况下,comboBox显示表引用。这种方式在数据验证列表上对我有用,但在不使用此宏的情况下也适用。

  2. 另一方面,尝试使用另一个函数 =OFFSET('Posibles valores'!$P$4;0;0;COUNTA('Posibles valores'!$P$4:'Posibles valores'!$P$1000);1)) 但它给出错误 you cannot use reference operators (such as unions,intersections,and intervals),array constants,or the LAMBDA function for Validation of specific data

这是带有一些注释的可搜索组合框的代码

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)
    Set nameRangeFormula = Application.ActiveWorkbook.Names(xStr)
    nameRangeFormula = Right(nameRangeFormula,Len(nameRangeFormula) - 1)
    Dim rangeFormula As Range
    'Application.ActiveSheet.Select
    'Set rangeFormula = Range(nameRangeFormula).Select
    
    Set rangeFormula = Names(nameRangeFormula).RefersToRange
    If xStr = "" Then Exit Sub
    With xComBox
        .Visible = True
        .Left = Target.Left
        .Top = Target.Top
        .Width = Target.Width + 5
        .Height = Target.Height + 5
        '.ListFillRange = rangeFormula
        .ListFillRange = xStr
        If .ListFillRange = "" Then
            xArr = Split(xStr,",")
            Me.TempCombo.List = xArr
        End If
        .LinkedCell = Target.Address
    End With
    xComBox.Activate
    Me.TempCombo.DropDown
End If
Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,ByVal Shift As Integer)
    Select Case KeyCode
        Case 9
            Application.ActiveCell.Offset(0,1).Activate
        Case 13
            Application.ActiveCell.Offset(1,0).Activate
    End Select
End Sub

我对 VBA 的了解并不比我过去两周学到的多。首先,我想知道是否有某种方法可以执行函数并返回范围。如果我认为解决此问题的唯一方法是创建一个以表头引用为参数的方法创建一个范围。

最后两件事,searchable 的宏功能用于每一列,并通过列表验证代码在其中。有什么方法可以选择独占它们可以使用的工作表和列。此代码允许给出错误的结果。它只是用一个绿色的角落突出显示错误的字段,如果它是错误的,任何方法都可以删除,或者像认数据验证一样弹出窗口。

提前致谢

编辑:在执行第一个If Target.Column = 3 Or Target.Column = 4 Then 之前,我成功地为我想要的列激活了组合框 if。据我所知,每个“页面”都有自己的代码。这是正确的吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)