使用INDEX和MATCH VBA在左侧Reverse Vlookup中查找值

问题描述 投票:1回答:5

我见过很多论坛说可以在左栏找到数据,而不是Vlookup函数。

我有一张表,我想从左边得到价值。换句话说,我想在右边搜索一个序列号,比如第3列,并在第1列获得一个值。

在VBA中,Vlookup可以替代什么? (即反向vlookup)

我看过几个使用索引匹配的示例论坛,但我无法理解语法。有人可以给我一个简单的例子吗?

这是我之前使用的,发现Vlookup无法向左搜索:

Sal = Application.WorksheetFunction.VLookup("3491709101",Sheets(PreviousTabName).Range(ThisRAnge), 2, False)
excel vba vlookup worksheet-function
5个回答
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))

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

0
投票

我会用VLOOKUP替换Application.Match,但是你需要添加一个错误处理程序场景,以防q​​azxswpoi失败。

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 ,这是一个转换为String的数值,因此如果"3491709101"中的源数据未设置,则相同(作为String)匹配将失败。


0
投票

看看你对@Mistella的评论,我认为你不需要ThisRangeVlookup。我的理解是你想在第8列中找到序列号Match,并将值从"3491709101"设置为Variable Columns(2)。如果我是对的,那么简单的查找就是你需要的。

Sal

0
投票

索引匹配语法VBA

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   

索引匹配语法FORMULA

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))

LEFT VLOOKUP语法VBA

  • 返回错误不匹配

=INDEX(ReturnValueRange,MATCH(LookUpValue, LookupRange, 0))

LEFT VLOOKUP语法FORMULA

Result = WorksheetFunction.VLOOKUP(LookUpValue, WorksheetFunction.CHOOSE(Array(1,2), LookupRange, ReturnValueRange), 2, 0))
  • “CHOOSE”功能正在操纵这两个字段。
  • 您将参考“索引”编号分配给数据范围 - 将LookupRange分配给索引编号1,将ReturnValueRange分配给索引编号2
  • 因此,当您在VLookup函数中键入“2”时,实际上是指CHOOSE函数中的索引号2。
© www.soinside.com 2019 - 2024. All rights reserved.