如何在VBA中使用vlookup在该单元格中输入值

时间:2017-12-14 16:22:37

标签: excel vba excel-vba

我试图在命令按钮中使用VBA中的vlookup,在确定数据应该进入哪一行后输入一个值。有一个更好的方法吗?我希望按钮能够查找输入的名称并将新号码输入到相应的行中。

这是我尝试输入值的地方。
enter image description here

3 个答案:

答案 0 :(得分:0)

Sub MyReplace()
Dim rw As Long
With ActiveSheet
    On Error Resume Next
        rw = Application.WorksheetFunction.Match(.Range("B13"), .Range("B4:B7"), 0)
    On Error GoTo 0
    If rw > 0 Then
        .Range("C" & rw + .Range("B4:B7").Row - 1).Value = .Range("C13")
    Else
        MsgBox "Name not found in range"
    End If
End With
End Sub

答案 1 :(得分:0)

没有错误处理程序:

Sub F()
    With [B3:C7]
        .Cells(WorksheetFunction.Match([B13], .Columns(1), 0), 2) = [C13]
    End With
End Sub

答案 2 :(得分:0)

如果该按钮是ActiveX控件,您可以点击工作表模块CommandButton_Click事件,并将以下子项添加到工作表本身。

Option Explicit

Private Sub CommandButton1_Click()  'Change CommandButton1 to the actual name of your button
    Dim i As Long
    Dim blnRecorded As Boolean

    With Me
        For i = 4 To .Cells(4, 2).End(xlDown).Row
            If .Cells(i, 4) = .Cells(4, 3) Then
                .Cells(i, 5) = .Cells(4, 4)
                blnRecorded = True
                Exit For
            End If
        Next i

        If Not blnRecorded Then
            MsgBox "The typed name does not match any employee in the table"
        End If
    End With

End Sub