我见过很多论坛都在说,与Vlookup
函数相反,可以在左列查找数据。
我有一张纸,我想从左边获取数值。换句话说,我想在右边寻找一个序列号,例如第3列,并在第1列获取一个值。
在VBA中,Vlookup
的替代方案是什么? (即反向vlookup)
我看过几个使用索引匹配示例的论坛,但是我不理解其语法。有人可以给我提供一个简单的例子吗?
这是我以前使用的,发现Vlookup无法向左搜索:
Sal = Application.WorksheetFunction.VLookup("3491709101",Sheets(PreviousTabName).Range(ThisRAnge), 2, False)
答案 0 :(得分:0)
我将使用索引匹配组合。这样的事情应该起作用:
Sal = Application.WorksheetFunction.INDEX(Sheets(PreviousTabName).Range(ThisRAnge),Application.WorksheetFunction.MATCH("3491709101",Application.WorksheetFunction.INDEX(Sheets(PreviousTabName).Range(ThisRAnge),0,3),0),1)
或者,假设第1列的引用存储在变量1stColumn
中,第3列存储在3rdColumn
中,则代码应为:
Sal = Application.WorksheetFunction.INDEX(1stColumn,Application.WorksheetFunction.MATCH("3491709101",3rdColumn,0))
答案 1 :(得分:0)
我想用 [sic] 搜索右边的序列号,例如第3列,并在第1列获取一个值。
使用Application.Match检索行号。使用Application.Index有点过分,因为Cell也会做得很好。
dim n as variant, val as variant
n = application.match("3491709101", Sheets(PreviousTabName).Range("C:C"), 0)
if not iserror(n) then
val = Sheets(PreviousTabName).Cells(n, "A").Value
debug.print val
end if
答案 2 :(得分:0)
我将VLOOKUP
替换为Application.Match
,但是您需要添加一个错误处理程序,以防Application.Match
失败。
代码
Option Explicit
Sub UseMatch()
Dim PreviousTabName As String
Dim SourceSht As Worksheet
Dim ThisRange As Range
Dim LastRow As Long, MatchRow As Variant
Dim Val
' set the worksheet object where you have the data you are trying to match it with
Set SourceSht = ThisWorkbook.Sheets("Sheet2") ' <-- modify "Sheet2" to your sheet's name
' set the range where you want to search for a Match
With SourceSht
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row ' find last row in column "C" , you can change to the column you need
' set dynamic Range
Set ThisRange = .Range(.Cells(1, "C"), .Cells(LastRow, "C")) ' change the column to where your data you are trying to compare lies
End With
If Not IsError(Application.Match("3491709101", ThisRange, 0)) Then
MatchRow = Application.Match("3491709101", ThisRange, 0) ' row of match found
Val = SourceSht.Range("A" & MatchRow) ' getting the value 2 columns to the left of column "C"
MsgBox Val
Else
' Match error, raise a msgbox
MsgBox "Unable finding 3491709101 in range " & ThisRange.Address(0, 0, xlA1), vbCritical, "Error"
End If
End Sub
注意:在处理数值时,您需要确保两个位置的格式相同。
例如,您正在寻找"3491709101"
,这是一个转换为String的数字值,因此,如果未设置ThisRange
中的源数据,则相同的(与String一样)匹配将失败。
答案 3 :(得分:0)
看您对@Mistella的评论,我认为您不需要Vlookup
或Match
。我的理解是,您想在第8列中找到序列号"3491709101"
,并将值从Columns(2)
设置为变量Sal
。如果我是对的,那么您需要一个简单的“查找”。
Dim rng As Range, findVal As Variant, Sal As Variant
Dim PreviousTabname As Worksheet, ThisRange As Range
Set rng = Sheets(PreviousTabname).Range(ThisRange)
Set findVal = rng.Find(What:="3491709101", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If findVal Is Nothing Then
Exit Sub
Else: Sal = findVal.Offset(, -6).Value
End If
MsgBox Sal
答案 4 :(得分:0)
索引匹配语法VBA
Dim ReturnValueRange as range, LookupRange as range
Set ReturnValueRange = Thisworkbook.Sheets("Sheet1").range("$E$1:$E$7")
Set LookupRange = Thisworkbook.Sheets("Sheet1").range("$G$1:$G$7")
LookUpValue = "findMe"
Result = Application.WorksheetFunction.INDEX(ReturnValueRange,Application.WorksheetFunction.MATCH(LookUpValue, LookupRange, 0))
索引匹配语法“ FORMULA”
=INDEX(ReturnValueRange,MATCH(LookUpValue, LookupRange, 0))
LEFT VLOOKUP语法VBA
Result = WorksheetFunction.VLOOKUP(LookUpValue, WorksheetFunction.CHOOSE(Array(1,2), LookupRange, ReturnValueRange), 2, 0))
LEFT VLOOKUP语法公式
=VLOOKUP(LookUpValue, WorksheetFunction.CHOOSE({1,2}, LookupRange, ReturnValueRange), 2, 0))