简体   繁体   English

当第一个单元格上的值发生变化时,Excel VBA 代码复制一行中的一系列单元格并粘贴到不同的工作表但相同的行/范围

[英]Excel VBA code to copy a range of cells in a row and paste in a different sheet but same row/range, when the value on 1st cell changes

I need a vba code to copy a range of cells in a row (eg:Sheet1 ! A2:I2), and paste it in a different sheet (eg "Sheet2") but same row (ieSheet 2! A2:I2), need this to happen automatically when a value in the column "K2" is changed, note that K2-K100 contain values that can change.我需要一个 vba 代码来复制一行中的一系列单元格(例如:Sheet1!A2:I2),并将其粘贴到不同的工作表(例如“Sheet2”)但同一行(即Sheet 2!A2:I2)中,需要这会在“K2”列中的值更改时自动发生,请注意 K2-K100 包含可以更改的值。

So if K3 of sheet 1 changes, then A3:I3 of Sheet 1 should automatically be copied to A3:I3 of Sheet 2, likewise if K4 of sheet 1 changes, then A4:I4 of sheet 1 should automatically be copied to A4:I4 of sheet 2所以如果sheet 1的K3改变,那么sheet 1的A3:I3应该自动复制到sheet 2的A3:I3,同样如果sheet 1的K4改变,那么sheet 1的A4:I4应该自动复制到A4:I4第 2 页

Any kind suggestions;任何善意的建议;

PS: New to vba work, apologies if I haven't written out my request properly PS:vba 新手,如果我没有正确写出我的请求,请见谅

"Worksheet_Change": Automatic (Event Driven) Copying “Worksheet_Change”:自动(事件驱动)复制

  • Copy this code to the worksheet being monitored and change the values in the constants section particularly 'strPaste' ie the name of the worksheet to be written to.将此代码复制到被监视的工作表并更改常量部分中的值,特别是“strPaste”,即要写入的工作表的名称。
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:  If data changes in cells of a specified column in the worksheet    '
'           containing this code, automatically (event driven) copies          '
'           the row ranges determined by the rows of the changed cells         '
'           and other specified conditions to the same row ranges on another   '
'           specified worksheet.                                               '
' Remarks:  This worksheet will be monitored, another will be written to.      '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Worksheet_Change(ByVal Target As Range)

    ' Constants (change to fit your needs)
    Const strPaste As String = "Sheet2"   ' Paste WorkSheet Name
    Const FR As Long = 2                  ' First Row Number
    Const FC As Long = 1                  ' First Column Number
    Const LC As Long = 9                  ' Last Column Number
    Const CC As Long = 11                 ' Criteria Column Number

    Dim wsPaste As Worksheet              ' Paste Worksheet
    Dim rngC As Range                     ' Criteria Column Range
    Dim rngCC As Range                    ' Current Criteria Cell Range
    Dim LR As Long                        ' Last Row Number
    Dim RCO as long                       ' Resize ColumnSize

    ' Prevent events staying disabled if something goes wrong.
    On Error GoTo ProcedureExit

    ' Caclulate Last Row Number (LR).
    LR = Me.Cells(Me.Rows.Count, CC).End(xlUp).Row
    ' Initialize Criteria Column Range (rngC).
    Set rngC = Me.Cells(FR, CC).Resize(LR - FR + 1)

    ' Check if there has been a change in Criteria Column Range (rngC).
    If Not Intersect(Target, rngC) Is Nothing Then

        On Error Resume Next
            ' Initialize Paste Worksheet (wsPaste).
            Set wsPaste = Worksheets(strPaste)
            ' Check if Paste Worksheet (wsPaste) was initialized.
            If wsPaste Is Nothing Then GoTo WorksheetError
        On Error GoTo ProcedureExit

        ' Calculate Resize ColumnSize (RCO).
        RCO = LC - FC + 1

        ' Disable events to speed up write operations.
        Application.EnableEvents = False

        ' Loop through found Criteria Cells (rngCC).
        For Each rngCC In Intersect(Target, rngC)
            ' Copy values from this worksheet (Me) to Paste Worksheet (wsPaste).
            wsPaste.Cells(rngCC.Row, FC).Resize(, RCO).Value _
              = Me.Cells(rngCC.Row, FC).Resize(, RCO).Value
        Next

    End If

ProcedureExit:
    ' Enable events.
    Application.EnableEvents = True

Exit Sub

WorksheetError:
    MsgBox "There is no worksheet named '" & strPaste & "'. " & vbCrLf _
      & "Change 'Paste WorkSheet Name' ('strPaste') in VBA (Alt+F11).", _
      vbCritical, "Wrong Worksheet Name"
    GoTo ProcedureExit

End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

暂无
暂无

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

相关问题 Excel VBA 复制一张表的第一行数据并粘贴到另一张表 - Excel VBA to copy 1st row of data of one sheet and paste to another sheet 如果同一行中的一系列单元格发生任何更改,Excel VBA将更新单元格中的日期 - Excel VBA update the date in a cell if anything changes in a range of cells in the same row 如果某个范围中的单元格的值为“ x”,则将某个范围的单元格复制并粘贴到另一张工作表中 - If cell in a range has value “x” copy and paste a range of cells into a different sheet VBA 将范围复制到最后一行并粘贴到新工作表上,单元格 A19 之后的第一个空行 - VBA copy range to last row & paste on new sheet, first empty row after cell A19 Excel 2007复制范围值并粘贴到另一个范围(同一张纸)的第一个空行 - Excel 2007 Copy range values and paste to another range (Same Sheet) first empty row Excel VBA宏-将单元格和粘贴值复制到活动单元格范围 - Excel VBA Macro - Copy Cells and Paste Values to Active Cell Range Excel VBA - 从一系列单元格中复制并粘贴到一个单元格中 - Excel VBA - Copy from a range of cells and paste in one cell 复制一个单元格并粘贴到一系列单元格中的第一个空白行 - Copy a cell and paste into first blank row in a range of cells 如果表格范围内的单元格值等于“新建”,则复制整行并粘贴为工作表 1 中下一个空单元格中的值 - If cell value in table range equals “New” copy entire row and paste as values in sheet 1 in the next empty cell Excel:需要复制和粘贴单元格,取决于范围中一行中单元格值的非空白状态 - Excel: Need to copy and paste cells depending on non-blank status of cell values in one row in range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM