在用户定义的范围内返回行号,而不是绝对行号

我有范围rngData和数组aCopyrows() [int]

以下循环用于填充数组:

 For Each c In rngData If c.Value = "Search term 1" Then aCopyrow(1) = c.Row ElseIf c.Value = "Search term 2" Then aCopyrow(2) = c.Row ElseIf c.Value = "Search term 3" Then aCopyrow(3) = c.Row End If Next c 

我想让数组存储与rngData相对应的行号, rngData ,如果在rngData的第一行中find匹配rngData ,则整数1存储在aCopyrow 。 但是,使用此方法存储该特定单元格的全局行号。

任何帮助感激!

rngData.c.Row只是给我一个错误(方法不支持)。

怎么样:

 aCopyrow(1) = c.Row - rngData.Cells(1,1).Row + 1 

如果有3个以上的ifs,你可以像这样迭代来使代码更紧凑:

 Dim i as Long For Each c In rngData For i = 1 To 3 If c.Value = "Search term " & i Then aCopyrow(i) = c.Row - rngData.Cells(1,1).Row + 1 End If Next i Next c 

使用一个数组的数组,它会快很多:

 Dim vArray As Variant vArray = rngData.Value For lRow = 1 To UBound(vArray, 1) For lCol = 1 To UBound(vArray, 2) If vArray(lRow, lCol) = "Search term 1" Then aCopyrow(1) = lRow ElseIf vArray(lRow, lCol) = "Search term 2" Then aCopyrow(2) = lRow ElseIf vArray(lRow, lCol) = "Search term 3" Then aCopyrow(3) = lRow End If Next lCol Next lRow