简体   繁体   English

当指定列中的单元格更改时,复制表的一行

[英]Copy a row of a table when a cell in a specified column changes

I'm trying to copy the row in a table when a cell in a specified column has data inserted then paste this row into another sheet. 当指定列中的单元格插入数据时,我试图复制表中的行,然后将此行粘贴到另一张表中。

The table starts at cell A3 being the first header to the table and it is 9 columns long, there will be an endless amount of rows. 该表从作为该表的第一个标题的单元格A3开始,它的长度为9列,将有无数行。

The column to monitor for change is column 8, named "Date Complete". 监视更改的列是名为“日期完成”的列8。 The information entered should always be a date, format "dd mmm". 输入的信息应始终为日期,格式为“ dd mmm”。

The row needs to be copied onto a sheet with the same name as the date entered into column 8 which may not exist before the date is entered. 该行需要复制到与输入第8列的日期同名的工作表上,该日期在输入日期之前可能不存在。

Also before the copying is done I would like a text box to enter notes into the corresponding cell in column 9, named "Notes". 同样,在完成复制之前,我想在第9列的相应单元格中将笔记输入笔记,即“笔记”。

Private Sub Worksheet_change(ByVal Target As Range)

   Const lngdatecomplete As Long = 8

   Dim wks As Worksheet

   Dim lngNextAvailableRow As Long

   If Target.Areas.Count = 1 And Target.Cells.Count = 1 Then

      If Not Intersect(Target, Columns(lngdatecomplete)) Is Nothing Then                            

         On Error Resume Next
         Set wks = ThisWorkbook.Worksheets(Target.Value)
         On Error GoTo 0

         If wks Is Nothing Then

            lngNextAvailableRow = wks.Range("a1").CurrentRegion.Rows.Count + 1
            ActiveSheet.Range(Cells(Target.Row, 2), Cells(Target.Row, 8)).copy _
             wks.Range("A" & lngNextAvailableRow).PasteSpecial

         ElseIf Not wks Is Nothing Then

            Dim ShtName$

            Sheets.Add after:=Sheets(Sheets.Count)

            ShtName = Format(Date, "dd mmm")

            Sheets(Sheets.Count).Name = ShtName

            Sheets(ShtName).Visible = True

            lngNextAvailableRow = wks.Range("a1").CurrentRegion.Rows.Count + 1
            ActiveSheet.Range(Cells(Target.Row, 2), Cells(Target.Row, 8)).copy _
             wks.Range("A" & lngNextAvailableRow).PasteSpecial

         End If
      End If
   End If
End Sub

The following seems pretty robust and will accept multiple values pasted into column H. I would advise setting a breakpoint on the Application.EnableEvents = False code line and typing a date into column H. Once you arrive at the breakpoint, you can step through each line with the F8 key. 下面的代码看起来很健壮,并且可以接受粘贴到H列中的多个值。我建议在Application.EnableEvents = False代码行上设置一个断点,并在H列中输入一个日期。一旦到达断点,就可以逐步执行每个操作按F8键。

Private Sub Worksheet_change(ByVal Target As Range)
    Const lDATECMPLT As Long = 8

    If Not Intersect(Target, Columns(lDATECMPLT)) Is Nothing Then
        On Error GoTo bm_Safe_Exit
        'Application.ScreenUpdating = False
        Application.EnableEvents = False
        Dim trgt As Range
        For Each trgt In Intersect(Target, Columns(lDATECMPLT))
            If trgt.Row > 3 And IsDate(trgt) Then
                trgt.NumberFormat = "dd mmm"
                On Error GoTo bm_Need_WS
                With Worksheets(trgt.Text)
                    On Error GoTo bm_Safe_Exit
                    trgt.Resize(1, 7).Offset(0, -6).Copy _
                      Destination:=.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                    'optional mark the row copied
                    'With trgt.Resize(1, 7).Offset(0, -6).Font
                    '    .Strikethrough = True
                    '    .Color = RGB(120, 120, 120)
                    'End With
                End With
            End If
        Next trgt
    End If
    GoTo bm_Safe_Exit

bm_Need_WS:
    On Error GoTo 0
    With Worksheets.Add(after:=Sheets(Sheets.Count))
        .Name = trgt.Text
        .Visible = True
        .Cells(1, 1).Resize(1, 7) = Me.Cells(3, 2).Resize(1, 7).Value2
        With ActiveWindow
            .SplitColumn = 0
            .SplitRow = 1
            .FreezePanes = True
            .Zoom = 80
        End With
    End With
    Resume

bm_Safe_Exit:
    Application.EnableEvents = True
    Me.Activate
    Application.ScreenUpdating = True
End Sub

I left some extras like copying the headers from the original worksheet into the new worksheet, freezing row 1 on the new worksheet, zooming the new worksheet, etc. Delete or adjust these these if you do not find them helpful. 我留下了一些额外的信息,例如将标题从原始工作表复制到新工作表中,冻结新工作表上的第1行,缩放新工作表等。如果您认为这些内容不起作用,请删除或调整这些内容。

When you have made all adjustments to the code, uncomment the 'Application.ScreenUpdating = False code line to avoid screen flashes. 对代码进行所有调整后,请取消注释'Application.ScreenUpdating = False代码行,以避免屏幕闪烁。

暂无
暂无

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

相关问题 如果特定列中的单元格发生更改,则从表中复制行 - Copy row from table if a cell in a specific column changes 如何在单元格值更改时交替表格中的行颜色? - How to alternate row color in a table when cell value changes? 值(Col C,第n行)更改时,将关键单元格(Col A,第n行)复制到审核日志 - Copy Key Cell (Col A, Row n) to Audit Log when value (Col C, row n) changes 单元格值更改时,将单元格值更改所在的列复制到相同范围的另一张工作表 - When a cell value changes, copy the column from where the cell value changed to another sheet at the same range 在具有行,列格式的单元格中复制索引公式 - Copy Index formular in cell with row,column format 将单元格值复制并粘贴到列的最后一行 - Copy and paste a cell value on the last row of a column 基于行和列复制粘贴单元格 - Copy Paste a cell based on Row & Column 当活动工作表列中的任何单元格更改时,执行Excel宏以将所有工作表复制到主工作表 - Execute Excel Macro to Copy All Sheets to Master Sheet When Any Cell in Active Sheet Column Changes 将单元格数据复制到新的表格行 - Copy cell data to a new table row 如何使用分配给按钮的代码将隐藏在表格底部的行中的公式复制到位于同一列中的活动单元格? - How to copy a formula hidden in a row at the bottom of a table to the active cell located in the same column using code assigned to a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM