简体   繁体   English

从一张纸上复制一定范围的单元格,然后插入下一个空行

[英]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. 我想将一行(A1:B1)中的数据复制到另一张纸的下一个空行中,并删除该行。 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. 我也希望每当(A1:B1)被编辑时都会发生这种情况。 The macro should initialize itself whenever A1:B1 is edited. 只要编辑了A1:B1,宏就应该初始化自己。

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: 看完OP的工作簿后,这是我提供的过程:

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

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

相关问题 我想将单元格的范围从一张纸复制到下一张空白行的另一张纸 - I want to copy a range of cells from one sheet to another sheet in the next blank row 从范围复制并过去到下一个空单元格中的另一个工作表中 - Copy from a range and past in another sheet in the next empty cell in a row Excel VBA-将范围从一张纸复制到另一张纸,下一个空行 - Excel VBA - Copy range from one sheet to another, next empty row 将数据从一张纸复制到下一个空行的另一张纸上 - copy data from one sheet to another on the next empty row 将单元格范围从一张纸复制到另一张纸 - Copy range of cells from one sheet to another 将特殊单元格复制到另一张纸上的下一个空行 - Copy Special Cells to next empty row on another sheet 从工作表 1 复制范围并粘贴到循环中下一张工作表的下一个空行 - Copy a range from sheet 1 and paste to next empty row on next sheets in loop 复制列直到一张纸的最后一行,然后粘贴到另一张纸的下一个空行 - Copy columns until last row from one sheet and paste to the next empty row of another sheet 尝试从一张纸上复制一个范围并将其粘贴到另一张纸上一列中的下一个空单元格 - Trying to copy a range from one sheet and paste it to the next empty cell in a column on another sheet 将特定单元格从sheet2sheet复制到下一个空白行 - Copy Specific Cells from sheet2sheet to the next blank row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM