如何进入单元格来更改值?

时间:2017-02-14 12:43:44

标签: excel vba excel-vba

我做了一些改变'需要转到Vlookup中找到的单元格的按钮,如下所示。

我只能查找到达给定范围的代码。

Private Sub CommandButton6_Click()

Dim status As Variant

status = WorksheetFunction.VLookup( _
        (Sheets("Gegevens").Range("B3")), _
         Sheets("bestand totaal").Range("J2:W9996"), 14, False)

End Sub

2 个答案:

答案 0 :(得分:1)

也许你在此之后

Private Sub CommandButton6_Click()
    Dim status As Variant

    status = Application.Match(Sheets("Gegevens").Range("B3"), _
                               Sheets("bestand totaal").Range("J2:J9996"), False) '<--| try and get the index of 'Sheets("Gegevens").Range("B3")' value in 'Sheets("bestand totaal").Range("J2:J9996")' range

    If Not IsError(status) Then '<--| if value successfully found
        With Sheets("bestand totaal") '<--| reference "target" sheet
            .Activate '<--| activate it
            .Range("W2:W9996").Cells(status, 1).Select <--| select corresponding value in column "W"
        End With
    End If
End Sub

答案 1 :(得分:0)

通过status找到VLookup后,您可以使用Find功能搜索找到值的VLookup范围。

我已为Range范围添加了2个Rng个变量VLookup,为RngFind添加了Find

<强>代码

Private Sub CommandButton6_Click()

Dim status As Variant
Dim Rng As Range
Dim RngFind As Range

Set Rng = Sheets("bestand totaal").Range("J2:W9996")
status = WorksheetFunction.VLookup((Sheets("Gegevens").Range("B3").Value), Rng, 14, False)

Set RngFind = Rng.Find(What:=status, LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
RngFind.Parent.Activate ' activate the worksheet, in case the VLookup Range is not the active sheet
RngFind.Select ' select the range (cell) where Find was set to true

End Sub