我需要使用从另一个工作表中查找列并返回单个单元格VBA . Sheet1中的我的数据是,
P.No. REV Qty
2918 01 50
2918 02 44
2919 01 72
在Sheet2中,它应该通过查看 P.No.
和 REV
列返回 Qty
. 请帮助我 .
我查找一列的代码如下 . 在这里我需要查找两列 .
Function SingleCellExtractInward(lookupvalue As String, lookuprange As Range, ColumnNumber As Integer)
Dim i As Double
Dim Result1 As String
Dim Result2 As String
If Result2 = Empty Then
Result2 = "no recent inward"
SingleCellExtractInward = Result2
End If
For i = 1 To lookuprange.Columns(1).Cells.Count
If lookuprange.Cells(i, 1) = lookupvalue Then
Result1 = Result1 & " " & lookuprange.Cells(i, ColumnNumber) & ","
SingleCellExtractInward = Left(Result1, Len(Result1) - 1)
End If
Next i
End Function
2 回答
lookupvalue
,所以我们有lookupValueA
和lookupValueB
:请注意,我将
Integer
更改为Long
. 除非您需要与旧API(see explanation here)进行通信,否则请始终使用Long
而不是Integer
.Alternative: 如果您希望能够另外只查看一个标准A或B,如果另一个为空,则使用:
在这种情况下,您可以保留
lookupValue
之一vbNullString
(与""
相同),它只查找一个条件(就像您的原始函数一样) .以下只是改进和删除我推荐的不必要的代码:
i
应为Long
而不是Double
:If Result2 = Empty Then
此时始终为真 . 所以我们甚至不需要Result2
.下列 …
可以减少到一行......
所以我们最终会得到类似的东西:
尝试使用此功能:
它使用数组公式来尝试匹配两个条件:strPNo和strRev . 在你的单元格中使用它,如:
如果找不到匹配项,它将显示#VALUE!保护它:
如果你愿意的话 .