Vlookup的局限在于它在单个列中搜索值。我需要搜索多列和多行。
我的数据格式如下:
HOST1 Guest1 Guest2 Guest3 Guest4
HOST2 Guest5 Guest6 Guest7 Guest8
我需要将它转换为两个列对,如下所示:
Guest1 Host1
Guest2 Host1
Guest3 Host1
所以我想在第一个例子中查找范围中的访客姓名b1:e2。 然后获取行号,并获取{A $ rownumber}的值。
是否可以进行这种多列,多行搜索?或者所有搜索都限于一维向量?
答案 0 :(得分:0)
双重查找的方法是使用INDEX(MATCH(...), MATCH(...))
。
在Excel 2003中,您甚至可以激活为您执行此操作的查找向导。
答案 1 :(得分:0)
我创建了一个名为ComboBox1的组合框和一个名为TextBox1的文本框(启用了多行属性)的用户表单。加载用户表单(UserForm1)时,Initialize事件将触发执行UserForm_Initialize()。这将在所有主机中填充组合框。当您选择主机时,ComboBox1_Change()事件将触发并输出HOST2 Guest5 Guest6 Guest7 Guest8
之类的文本框。但是,您可以将输出更改为您想要的任何位置。
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
你可以看到你如何修改它,这样你就可以遍历所有主机(即填充组合框时),并为每个主机调用ComboBox1_Change()事件(当然重命名为常规子)以输出所有在某个范围内的客人,您正在另一个工作表上进行迭代。
希望它有所帮助!