(vba)在单元格中为vlookup结果添加值

时间:2019-06-06 11:14:13

标签: excel vba

现在我有以下代码:

stockvalue = Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("In_bestelling").Range("G2").value, ThisWorkbook.Sheets("Producten").Range("B1:D9000"), 2, False)

因此返回5。 我需要做的是将vlookup获取的单元格的值设置为现在具有的值加上另一个值。

因此,在这种情况下,另一个值为10,vlookup发现5,因此需要在表示5的单元格中推入15。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以像这样使用相同的单元格值:

Dim stockvalue As Long
Dim ItemRow As Long

'wtih Application.Match we will find the row in column B that matches your item in G2
ItemRow = Application.Match(ThisWorkbook.Sheets("In_bestelling").Range("G2").Value, _
    ThisWorkbook.Sheets("Producten").Range("B1:B9000"), 0)
'Here we will change the value of the cell where the item was found with it's value + stockvalue
stockvalue = 10 'change this for whatever you need to add
With ThisWorkbook.Sheets("Producten")
    .Cells(ItemRow, 4) = .Cells(ItemRow, 4) + stockvalue
End With

答案 1 :(得分:0)

您可以尝试:

Option Explicit

Sub test()

    Dim wsIn As Worksheet, wsPro As Worksheet, rngFound
    Dim SearchString As String
    Dim FoundValue As Long, ResultValue As Long

    With ThisWorkbook
        Set wsIn = .Sheets("In_bestelling")
        Set wsPro = .Sheets("Producten")
    End With

    SearchString = wsIn.Range("G2").Value

    Set rngFound = wsPro.Range("B1:D9000").Find(What:=SearchString, LookIn:=xlValues, LookAt:=xlWhole)

    If Not rngFound Is Nothing Then

        With wsPro
            FoundValue = .Cells(rngFound.Row, rngFound.Column + 1).Value
            ResultValue = FoundValue + 10
            .Cells(rngFound.Row, rngFound.Column + 1) = ResultValue
        End With

    Else
        MsgBox "No Match!"
    End If

End Sub