Inserting data into the same row that a button is

2019-09-07 00:25发布

I have created a macro which copies data from one worksheet to another. I want a generalized macro which copies data from the same row number as that of button, instead of B2 as mentioned below in the code.

Currently this code is working fine; the button text is updated and MacroA has been assigned to it. I read about topleftcell, but am unable to implement it.

Sub MacroA()
'
' MacroA Macro
'
    Range("I2:J2").Select
    Selection.Copy
    Range("B2").Select
    Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
    Range("D2").Select
    Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWorkbook.Save
    ActiveWindow.Close
End Sub

1条回答
神经病院院长
2楼-- · 2019-09-07 01:06

Untested, but might help you along...

Sub Tester()

    Dim c As Range, sht As Worksheet
    Dim d As Range

    Set sht = ActiveSheet

    Set c = sht.Shapes(Application.Caller).TopLeftCell
    sht.Cells(c.Row, 2).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True

    With ActiveSheet
        Set d = .Cells.Find(What:="", After:=.Range("D2"), LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
        sht.Range("I2:J2").Copy d

        .Parent.Save
        .Parent.Close
    End With

    Application.CutCopyMode = False
End Sub
查看更多
登录 后发表回答