vlookup但是将返回不是单元格中的值的单元格的名称

时间:2017-03-29 04:32:56

标签: excel excel-vba vlookup vba

就像问你是否知道vlookup样式中的公式,但是它将返回的单元名称不是单元格的值?谢谢!

我想将sal的值放在Application.WorksheetFunction.VLookup(name, Sheet2.Range("Table6"), 5, False)中,但在此函数中,单元格内的值将不返回单元格的名称

Sub purchase()

Dim name As String
name = ActiveSheet.Range("C3")
sal = Application.WorksheetFunction.VLookup(name, Sheet2.Range("Table6"), 5, False) - ActiveSheet.Range("d3").Value

MsgBox " remaining: " & sal   

End Sub

1 个答案:

答案 0 :(得分:0)

VLookup仅返回一个值。您将需要match功能。这将返回可用于访问所需单元格的行号。

Sub purchase()
    Dim Name As String
    Name = ActiveSheet.Range("C3")

    Dim MatchedRow As Long
    MatchedRow = Application.WorksheetFunction.Match(Name, Sheet2.Range("Table6").Columns(1), 0)
      'Find the row number (relative to the range table6) of the found name

    Dim Sal As Double
    Sal = Sheet2.Range("Table6").Cells(MatchedRow, 5).Value - ActiveSheet.Range("D3").Value

    Sheet2.Range("Table6").Cells(MatchedRow, 5).Value = Sal
      'Write the calculated Sal back

    MsgBox " remaining: " & Sal
End Sub