简体   繁体   中英

Copy a range of cells from one sheet and insert into next empty row

I want to copy the data in a row(A1:B1) to the next empty row of another sheet and also delete the row. I have tried the following code but it is working only for a particular cell. Not sure how to do it for the next empty row. Can anybody help me?

Also I want this to happen whenever (A1:B1) is edited. The macro should initialize itself whenever A1:B1 is edited.

Sub Macro5()
'
' Macro5 Macro
'
' Keyboard Shortcut: Ctrl+w
'
    Range("A1:B1").Select
    Selection.Copy
    Sheets("Sheet1").Select
    Range("A32").Select
    ActiveSheet.Paste
    Sheets("Sheet2").Select
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub

In the code module for the primary sheet (this is the one you wish to copy from), place this routine:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 Then
        If Target.Column < 3 Then
            If Target.Row < 2 Then
                On Error Resume Next
                Application.EnableEvents = False
                With Sheet2
                    .Cells(.Rows.Count, "a").End(xlUp)(2).Resize(, 2) = [a1:b1].Value
                    [a1:b1].Delete xlUp
                End With
            End If
        End If
    End If
    Application.EnableEvents = True
End Sub

Note: this efficiently does what you asked for, but seems like an odd request...

UPDATE

After looking at OP's workbook, this is the procedure that I furnished:

Sub Transfer2ndSheetRow1To1stSheet()
    With Sheet2
        If .[counta(1:1)] Then
            Sheet1.Cells(.Rows.Count, "a").End(xlUp)(2).EntireRow = .[1:1].Value
            .[1:1].Delete xlUp
        End If
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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