2012-07-24 57 views
0

我創建了一個將數據從一個工作表複製到另一個工作表的宏。我想要一個通用宏,它可以從與按鈕相同的行號複製數據,而不是在代碼中提到的B2將數據插入到按鈕所在的同一行中

目前這段代碼工作正常;按鈕文本被更新並且MacroA已被分配給它。我讀了約topleftcell,但我無法實現它。

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 

回答

2

未經檢驗的,但可能會一起幫助你...

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 
相關問題