简体   繁体   English

VBA vlookup,剪切和粘贴

[英]VBA vlookup, cut & paste

I'm starting to think that I'm a mile off with this code. 我开始认为我与这段代码相距甚远。 I want to find the value in "r15" and find a match in the lookup table. 我想在“ r15”中找到值,并在查找表中找到匹配项。 Then, in that row and 2 columns to the right, I want to paste the value from e15. 然后,在该行和右侧的2列中,我要粘贴e15中的值。 Here's what I have: 这是我所拥有的:

Sub CopyX()
    MsgBox "FlightPlan for Profits PRO never actually DELETES an employee, It just marks an employee as inactive.  If the Employee were actually deleted from the database, archival records would not include a deleted employee (of course) and would therefore become inaccurate", vbInformation, "FlightPlan for Profits PRO"
    ActiveSheet.Range("e15").Select
    Selection.Copy
    vlookupVBA = Application.WorksheetFunction.vlookup(Range("r15"), Range("c24:c274"), 1, False)
        Range.Offset(0, 2).Select
    ActiveCell.PasteSpecial Paste:=xlPasteValues
End Sub

You were using the VLOOKUP function incorrectly. 您使用的VLOOKUP功能不正确。 In fact, a MATCH function would return the row number which was what you seemed to be looking for. 实际上, MATCH函数会返回您正在寻找的行号。

Sub CopyX()

    MsgBox "FlightPlan for Profits PRO never actually DELETES an employee." & Chr(10) & _
           "It just marks an employee as inactive. " & _
           "If the Employee were actually deleted from the database, archival records " & _
           "would not include a deleted employee (of course) and would therefore become inaccurate", _
           vbInformation, "FlightPlan for Profits PRO"

    With Worksheets("Sheet1")
        'first check if anything is there
        If CBool(Application.CountIf(.Range("c24:c274"), .Range("r15").Value)) Then
            'there is a row number to find; get it and set the value
            .Cells(Application.Match(.Range("r15").Value, .Range("c24:c274"), 0) + 23, "E") = _
                .Range("e15").Value
        End If
    End With

End Sub

With MATCH returning the position within C24:C274, 23 is added and that provides the correct row_number parameter for the Range.Cells property to receive the value from E15. 通过MATCH返回C24:C274中的位置 ,添加了23,它为Range.Cells属性提供了正确的row_number参数,以便从E15接收值。

I've added a line feed and broken your msgbox text into a couple of lines to keep it visible without right-scrolling. 我添加了一个换行符,并将您的msgbox文本分成几行,以使其可见而无需向右滚动。

Find will work for this as well. 查找也将为此工作。

Sub Button1_Click()
    Dim msg As String
    Dim lstRw As Long, rng As Range, c As Range

    lstRw = Cells(Rows.Count, "C").End(xlUp).Row
    Set rng = Range("C24:C" & lstRw)

    Set c = rng.Find(what:=Range("R15").Value, lookat:=xlWhole)

    msg = "FlightPlan for Profits PRO never actually DELETES an employee, " & vbNewLine
    msg = msg & "It just marks an employee as inactive. " & vbNewLine
    msg = msg & "If the Employee were actually deleted from the database, " & vbNewLine
    msg = msg & "archival records would not include a deleted employee (of course) " & vbNewLine
    msg = msg & " and would therefore become inaccurate"
    MsgBox msg, vbInformation, "FlightPlan for Profits PRO"

    If Not c Is Nothing Then
        Cells(c.Row, c.Column + 2).Value = Range("E15").Value
    Else: MsgBox "Not Found"
        Exit Sub
    End If


End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM