【发布时间】:2017-10-25 21:23:14
【问题描述】:
我正在尝试开发一个代码,该代码会带来用户选择的第 n 个匹配项,我已经找到了一个执行此操作的代码,但只有一列
我想获得字符串“castro”的第三次出现,但行值 a2 为“19”。有什么建议吗?
下面是我用来获取第二次出现但只使用一列的代码。
Sub test1()
Dim teste As String
teste = VLOOKUPNTH("prysmian", Range("B1:C22"), 2, 2)
End Sub
Function VLOOKUPNTH(lookup_value, table_array As Range, col_index_num
As Integer, nth_value)
Dim nRow As Long
Dim nVal As Integer
Dim bFound As Boolean
VLOOKUPNTH = "No Match"
With table_array
For nRow = 1 To .Rows.Count
If .Cells(nRow, 1).Value = lookup_value Then
nVal = nVal + 1
End If
If nVal = nth_value Then
VLOOKUPNTH = .Cells(nRow, col_index_num).Text
Exit Function
End If
Next nRow
End With
End Function
the table
A B C
a1 castro 1
a1 castro 3
a1 castro 4
a1 castro 5
a1 castro 6
a1 castro 7
a2 castro 17
a2 castro 18
a2 castro 19
a2 castro 20
a2 castro 21
a2 castro 22
a2 castro 23
【问题讨论】:
-
对不起,但这对我来说毫无意义。你想要第 3 行“Castro”(我假设在 c 列中有一个“4”),那么你如何进入“19”?我假设你表中的最后一个 row 是 13 (col c = 23)?
-
我想要第三个“castro”行,但在同一行中它的值是“a2”
-
并且是 'a2' 某个值,它将:(a)永远不会改变;将始终搜索“a2”;或 (b) 可以是“a3”或“a4”等?换句话说,您应该描述与您的搜索相关的规则......这对您解决代码问题的方式有很大的影响。
-
此外,您传递的范围不包括 A 列 - 这是可以找到“a2”的位置。这是有原因的吗?
-
我传递的范围代表我仅使用列 B 和 C 应用的代码...。此代码的规则是:
标签: vba match multiple-columns