带有多个标准的vlookup,返回的数组插入了ComboBox

发布于 2025-02-11 20:37:38 字数 572 浏览 2 评论 0原文

有没有一种方法可以使用VBA使用多个标准进行Vlookup,然后将这些返回的值插入ComboBox?对于下表,我想将“马林鱼”作为搜索条件,然后将{rbi,score,abv}插入combobox作为选项。这可能吗?

示例数据:

标识符价值
海盗得分
马林斯RBI
MARLINS得分
MarlinsABV
PiratesHRS
Application.WorksheetFunction.VLookup("Marlins", Worksheets("Metadata").Range("A2:B5"), 2, False)

Is there a way to use VBA to Vlookup with multiple criteria and then have those multiple returned values then inserted into a ComboBox? For the table below, I would like to have "Marlins" as the search criteria, and then have {RBI, Score, ABV} inserted into a ComboBox as options. Is this possible?

Example Data:

IdentifierValue
PiratesScore
MarlinsRBI
MarlinsScore
MarlinsABV
PiratesHRS
Application.WorksheetFunction.VLookup("Marlins", Worksheets("Metadata").Range("A2:B5"), 2, False)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末染殇 2025-02-18 20:37:38

填充一个Combobox

一个简单的例子

Sub PopulateComboBox()
    
    ' Reference the worksheet.
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Metadata")
    
    ' Write the values of the range to a 2D one-based array.
    Dim sData As Variant: sData = ws.Range("A2:B5").Value
    
    ' Write the unique values from column 2, where column 1 is "Marlin",
    ' to the keys of a dictionary.
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare ' case-insensitive
    Dim r As Long
    For r = 1 To UBound(sData, 1)
        If StrComp(CStr(sData(r, 1)), "Marlin", vbTextCompare) = 0 Then
           dict(sData(r, 2)) = Empty
        End If
    Next r
    
    ' Write the values from the keys of the dictionary to the combo box.
    With ws.ComboBox1 ' active-x combobox on the worksheet
        .Clear
        If dict.Count > 0 Then .List = dict.Keys
    End With
        
End Sub

Populate a ComboBox

A Simple Example

Sub PopulateComboBox()
    
    ' Reference the worksheet.
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Metadata")
    
    ' Write the values of the range to a 2D one-based array.
    Dim sData As Variant: sData = ws.Range("A2:B5").Value
    
    ' Write the unique values from column 2, where column 1 is "Marlin",
    ' to the keys of a dictionary.
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare ' case-insensitive
    Dim r As Long
    For r = 1 To UBound(sData, 1)
        If StrComp(CStr(sData(r, 1)), "Marlin", vbTextCompare) = 0 Then
           dict(sData(r, 2)) = Empty
        End If
    Next r
    
    ' Write the values from the keys of the dictionary to the combo box.
    With ws.ComboBox1 ' active-x combobox on the worksheet
        .Clear
        If dict.Count > 0 Then .List = dict.Keys
    End With
        
End Sub
守护在此方 2025-02-18 20:37:38

如果您处置VEST的较新的动态阵列功能。 MS365,
您可能会从以下函数过滤()

  • (a)返回评估的 filter()公式字符串(基于帮助函数的结果)代码> makeformula 和
  • (b)检查可能的非遇到
Function filtered(data As Range, Optional ByVal criteria As String = "Marlins")
'a) Return filtered data as 1-based 2D array.
    filtered = data.Parent.Evaluate(MakeFormula(data, criteria))
'b) (Provide for possible non-findings via Error check).
    If IsError(filtered) Then filtered = Array(Empty)
End Function

帮助函数makeformula

Function MakeFormula(data, ByVal criteria As String) As String
'0. Get Column addresses.
    Dim critAddr$: critAddr = data.Columns("A").Address(0, 0)
    Dim valsAddr$: valsAddr = data.Columns("B").Address(0, 0)
'1. Return concatenated formula - e.g. "FILTER(B2:B6,A2:A6=""Marlins"")"
    MakeFormula = "FILTER(" & valsAddr & "," & critAddr & "=""" & criteria & """)"
End Function

示例示例调用

足以传递整个数据是足够的 或可选标准,例如“马林鱼”)。

范围为参数(和/
获取(重新)由过滤值分配。

Sub ExampleCall
    ' Reference the worksheet.
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Metadata")
    ' Assign the filtered data to the combo box´es .List property.
    With ws.OLEObjects("ComboBox1")
        .Object.list = filtered(ws.Range("A2:B6"))
    End With
End Sub

If you dispose of the newer dynamic array features of vers. MS365,
you might profit from the following function filtered()

  • (a) returning an evaluated Filter() formula string (based on results of help function MakeFormula and
  • (b) checking for possible non-findings
Function filtered(data As Range, Optional ByVal criteria As String = "Marlins")
'a) Return filtered data as 1-based 2D array.
    filtered = data.Parent.Evaluate(MakeFormula(data, criteria))
'b) (Provide for possible non-findings via Error check).
    If IsError(filtered) Then filtered = Array(Empty)
End Function

Help function MakeFormula

Function MakeFormula(data, ByVal criteria As String) As String
'0. Get Column addresses.
    Dim critAddr$: critAddr = data.Columns("A").Address(0, 0)
    Dim valsAddr$: valsAddr = data.Columns("B").Address(0, 0)
'1. Return concatenated formula - e.g. "FILTER(B2:B6,A2:A6=""Marlins"")"
    MakeFormula = "FILTER(" & valsAddr & "," & critAddr & "=""" & criteria & """)"
End Function

Example call

It suffices to pass the entire data range as parameter (and/or an optional criteria, e.g. "Marlins").

Note that you don't need to clear the combobox values as the whole .List property
gets (re-)assigned by the filtered values.

Sub ExampleCall
    ' Reference the worksheet.
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Metadata")
    ' Assign the filtered data to the combo box´es .List property.
    With ws.OLEObjects("ComboBox1")
        .Object.list = filtered(ws.Range("A2:B6"))
    End With
End Sub

枕头说它不想醒 2025-02-18 20:37:38

使用ActiveX Combobox,并将默认名称作为ComboBox1。

Sub test()
Dim rg As Range: Dim cell As Range: dim crit as string

    With ActiveSheet
        Set rg = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
        .ComboBox1.Clear
    End With

crit = "Marlins"

    With rg
        .Replace crit, True, xlWhole, , False, , False, False
             For Each cell In .SpecialCells(xlConstants, xlLogical).Offset(0, 1)
                ActiveSheet.ComboBox1.AddItem cell.Value
             Next
        .Replace True, crit, xlWhole, , False, , False, False
    End With
    
End Sub

该代码假设您的数据与图像附件中的数据完全相同。
因此,标识符标头位于单元格A1中,其中数据从单元A2开始。
值标头位于单元B1中,其中数据从单元格B2开始。

过程:
它作为可变RG的标识符数据范围。

创建具有价值“马林鱼”的可变暴力。

然后在RG中,它替换所有包含文本“ Marlins”的行中的行,以逻辑true,获取包含“ true”偏移量的单元格(0,1),请执行循环以将每个循环的单元格值作为ComboBox1项目,将真正的价值带回“马林鱼”。

该子必须在数据表处于活动状态的情况下运行。

Use the ActiveX ComboBox, and let the default name as ComboBox1.

Sub test()
Dim rg As Range: Dim cell As Range: dim crit as string

    With ActiveSheet
        Set rg = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
        .ComboBox1.Clear
    End With

crit = "Marlins"

    With rg
        .Replace crit, True, xlWhole, , False, , False, False
             For Each cell In .SpecialCells(xlConstants, xlLogical).Offset(0, 1)
                ActiveSheet.ComboBox1.AddItem cell.Value
             Next
        .Replace True, crit, xlWhole, , False, , False, False
    End With
    
End Sub

The code assumed that your data exactly the same as in your image attachment.
So, the Identifier header is in cell A1 where it's data starts from cell A2.
And the Value header is in cell B1 where it's data starts from cell B2.

The process:
It make a range for the Identifier data as variable rg.

create variable crit with value "Marlins".

Then within the rg, it replace all rows which contains text "Marlins" with logical TRUE, get the range of the cell which contains "TRUE" offset(0,1), do the loop to put each looped cell value as the ComboBox1 item, bring the TRUE value back to "Marlins".

The sub must be run where the sheet of your data is active.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文