excel 使用宏按钮将行从一个工作表复制到另一个工作表

m0rkklqb  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(369)

我是新的excel和使用宏,所以请善良!
我希望从一个名为"列表"的工作表中复制整行,并将整行复制到另一个名为"已记录"的工作表中。复制的行应该转到"已记录"工作表中的下一个可用空行,然后从原始工作表中清除该行的内容。我希望能够使用宏按钮来执行移动操作。
有人能帮忙吗?
这段代码工作,但不是我想要的方式,在这个版本中,我必须在第一个工作表的A列输入一个"l",但我宁愿这样做,一个鼠标点击按钮:

Private Sub Worksheet_Change(ByVal Target As Range)

'   Check to see only one cell updated
    If Target.CountLarge > 1 Then Exit Sub
    
'   Check to see if entry is made in column B after row 5 and is set to "Yes"
    If Target.Column = 1 And Target.Row > 4 And Target.Value = "l" Then
        Application.EnableEvents = False
'       Copy columns B to M to complete sheet in next available row
        Range(Cells(Target.Row, "B"), Cells(Target.Row, "N")).Copy Sheets("logged").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
'       Delete current row after copied
        Rows(Target.Row).ClearContents
        Application.EnableEvents = True
       
        End If
End Sub
e4yzc0pl

e4yzc0pl1#

复制-清除行

    • 之前**

    • 之后**

第一节第一节第一节第二节第一节

    • 法典**
  • 复制多行时,无论单元格的选定顺序如何,复制行的顺序都与源工作表中的顺序相同。
Sub CopyClearRows()

    ' Constants
    Const SRC_NAME As String = "List"
    Const SRC_FIRST_ROW As String = "B5:N5"
    Const DST_NAME As String = "Logged"
    Const DST_FIRST_CELL As String = "B2"
    
    ' Workbook
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    If Not wb Is ActiveWorkbook Then Exit Sub ' workbook not active
    
    ' Source
    
    Dim sws As Worksheet: Set sws = wb.Sheets(SRC_NAME)
    If Not sws Is ActiveSheet Then Exit Sub ' worksheet not active
    If Not TypeOf Selection Is Range Then Exit Sub ' not a range
    
    Dim surg As Range
    ' Referencing all columns, not just the columns of the used range.
    Set surg = Intersect(Selection.EntireRow, sws.UsedRange)
    If surg Is Nothing Then Exit Sub ' selection rows outside of used range
     
    Dim srg As Range
    With sws.Range(SRC_FIRST_ROW)
        Set srg = Intersect(.Resize(sws.Rows.Count - .Row + 1), surg.EntireRow)
    End With
    If srg Is Nothing Then Exit Sub ' selected cells above given first row
    
    ' Destination
    
    Dim dws As Worksheet: Set dws = wb.Sheets(DST_NAME)
    If dws.FilterMode Then dws.ShowAllData
    
    Dim dfCell As Range: Set dfCell = dws.Range(DST_FIRST_CELL)
    With dfCell
        ' Either, not considering cells to the left...
        Dim dlCell As Range: Set dlCell = .Resize(dws.Rows.Count - .Row + 1, _
            dws.Columns.Count - .Column + 1) _
            .Find("*", , xlFormulas, , xlByRows, xlPrevious)
        ' ... or, also considering cells to the left...
        'Dim dlCell As Range: Set dlCell = .Resize(dws.Rows.Count - .Row + 1) _
            .EntireRow.Find("*", , xlFormulas, , xlByRows, xlPrevious)
        If Not dlCell Is Nothing Then
            Set dfCell = dws.Cells(dlCell.Row + 1, .Column)
        End If
    End With
    
    ' Copy & Clear.
    srg.Copy dfCell
    srg.ClearContents ' (not considering cells to the left or right)

    ' Inform.
    MsgBox "Rows copy-cleared.", vbInformation

End Sub

相关问题