简体   繁体   English

在Excel中跨多个列和行进行查找

[英]Lookup across multiple columns & rows in excel

Vlookup's limitation is that it searches for a value down a single column. Vlookup的局限性在于它在单个列中搜索值。 I need to search across multiple columns and multiple rows. 我需要搜索多个列和多个行。

I have data in the following format: 我有以下格式的数据:

HOST1    Guest1    Guest2    Guest3    Guest4
HOST2    Guest5    Guest6    Guest7    Guest8

I need to convert it down to two column pairs like this: 我需要将其转换成两个这样的列对:

Guest1    Host1
Guest2    Host1
Guest3    Host1

So I want to lookup the guest's name in the range, b1:e2 in the first example. 因此,我想在第一个示例中的b1:e2范围内查找来宾的姓名。 Then grab the row number, and grab the value of {A$rownumber}. 然后获取行号,并获取{A $ rownumber}的值。

Is it possible to do this kind of multicolumn, multirow search? 可以进行这种多列,多行搜索吗? Or are all the searches limited to one-dimensional vectors? 还是所有搜索都限于一维矢量?

The way for double lookups is to use INDEX(MATCH(...), MATCH(...)) . 双重查询的方法是使用INDEX(MATCH(...), MATCH(...)) in Excel 2003 you can even activate the Lookup Wizard that does it for you. 在Excel 2003中,您甚至可以激活为您执行的查找向导。

I made a user form with a combobox called ComboBox1 and a textbox called TextBox1 (multiline property enabled). 我用一个名为ComboBox1的组合框和一个名为TextBox1的文本框(启用了多行属性)制作了一个用户表单。 When you load the user form (UserForm1) you the Initialize event will fire executing UserForm_Initialize(). 当您加载用户窗体(UserForm1)时,初始化事件将触发执行UserForm_Initialize()。 That will populate the combobox with all the hosts. 这将用所有主机填充组合框。 When you select a host the ComboBox1_Change() event will fire and output something like HOST2 Guest5 Guest6 Guest7 Guest8 to the text box. 当您选择一个主机时,ComboBox1_Change()事件将触发并将诸如HOST2 Guest5 Guest6 Guest7 Guest8到文本框中。 But ofcourse you can change the output to be anywhere you want. 但是,当然,您可以将输出更改为所需的任何位置。

Option Explicit

Dim allHosts As Range
Private pHostRow As Integer
Dim hostColumn As Integer

Private Sub UserForm_Initialize()

    Dim Host As Range

    Dim firstHost As Range
    Dim lastHost As Range

    Dim lastHostRow As Integer

    hostColumn = 1

    Set firstHost = Cells(1, hostColumn)
    lastHostRow = firstHost.End(xlDown).Row

    Set lastHost = Cells(lastHostRow, hostColumn)
    Set allHosts = Range(firstHost, lastHost)

    For Each Host In allHosts
        ComboBox1.AddItem Host.Text
    Next Host

End Sub

.

Private Sub ComboBox1_Change()

    Dim selectedHost As String
    selectedHost = ComboBox1.Text

    pHostRow = allHosts.Find(selectedHost).Row

    Dim guest As Range
    Dim allGuests As Range
    Dim firstGuest As Range
    Dim lastGuest As Range
    Dim lastGuestCol As Integer

    Dim Host As Range
    Set Host = Cells(pHostRow, hostColumn)
    lastGuestCol = Host.End(xlToRight).Column

    Set firstGuest = Host.Offset(0, 1)
    Set lastGuest = Cells(pHostRow, lastGuestCol)
    Set allGuests = Range(firstGuest, lastGuest)

    TextBox1.Text = selectedHost

    For Each guest In allGuests
        TextBox1.Text = TextBox1.Text & selectedHost & guest.Text & vbCrLf
         'if you weren't outputting this to a textbox you wouldn't use the vbCrLf,
         'instead you would iterate to the next line in your output range.
    Next guest

End Sub

You can see how you can modify this so you would iterate through all the hosts (ie while populating the combobox) and for each host call the ComboBox1_Change() event (renamed of course and made a regular sub) to output all the guests to some range that you are iterating through on another worksheet. 您可以看到如何修改此设置,以便遍历所有主机(即在填充组合框时),并对每个主机调用ComboBox1_Change()事件(当然,已重命名并作了常规子例程)以将所有来宾输出到某些主机。您在另一个工作表上迭代的范围。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM