替换不同工作表中的值

Replacing value in different worksheet

我有一个问题,似乎无法解决。我花了几个小时尝试修复它,但我似乎无法通过错误。想必你修起来真的很简单

如果你能帮助我,那就太好了。我需要一个 VBA 代码来用不同工作中的值替换值 X(通过 vlookup 找到的单元格)sheet。

在 sheet 中:Voorraadmutatie doorvoeren 我在单元格 D4 中有一个值对应于 sheet Voorraadbeheer 中的位置stelling(zh)(见图片)。

单元格 D16 中的值是 new 股票,因此激活宏后数据库中的股票需要更改。但是,要找到要更改的正确单元格,我需要查找正确的 row/cell.

Sheet voorraadmutatie doorvoeren

Sheet Voorraadbeheer stelling(en)

如果你能帮助我,那就太好了。我尝试了很多东西,但我没有接受过使用 VBA 的培训。我不断收到错误消息,似乎无法通过浏览互联网解决问题。

提前致谢。

希望这个例子能帮到你

  Sub vLookup()
 
    Dim SearchedValue, oldStock, newStock As Variant
    Dim MyMatrix As Range
    Dim No_index_col As Single
    Dim ApproximateValue As Boolean
    Dim n As Long
    
    ' SearchedValue = "D002"
    SearchedValue = ThisWorkbook.Sheets("voorraadmutatie doorvoeren").Range("D4")
    ' new stock in D16
    newStock = ThisWorkbook.Sheets("voorraadmutatie doorvoeren").Range("D16")
    
    Set MyMatrix = ThisWorkbook.Sheets("voorraadbeheer stelling(en)").Range("A:C")
    No_index_col = 3
    ApproximateValue = False
    ' display the previous vlue of the stock
    oldStock = Application.WorksheetFunction.vLookup(SearchedValue, MyMatrix, No_index_col, ApproximateValue)
    
    n = 2 '2nd ligne of the matrix
    Do While Not (IsEmpty(ThisWorkbook.Application.Sheets("voorraadbeheer stelling(en)").Cells(n, 1)))

        If ThisWorkbook.Application.Sheets("voorraadbeheer stelling(en)").Cells(n, 1) = SearchedValue Then
 
            ThisWorkbook.Application.Sheets("voorraadbeheer stelling(en)").Cells(n, 3) = ThisWorkbook.Sheets("voorraadmutatie doorvoeren").Range("D16").Value
 
        End If
        ' next line
        n = n + 1
    Loop
    End Sub