Update cell, automatically copy row to a separate sheet

后端 未结 2 1640
误落风尘
误落风尘 2021-01-25 23:38

I have a worksheet comprising of two columns (A and B) ... the first of which is just a name, the second is a number.

If I make an edit to a number in column B, I want E

相关标签:
2条回答
  • 2021-01-26 00:03

    Here is some additional code to check the row number, as per the above answer and comments.

    Dim row_num As Long
    row_num = Cells(Rows.Count, "B").End(xlUp).Row
    If row_num > 1 then row_num = row_num + 1 'Add 1 only when row number doesn't equal to 1, otherwise - 1.
    
    0 讨论(0)
  • 2021-01-26 00:13

    Try this in the sheet where you have data (under the Excel Objects), e.g Sheet1

    Option Explicit
    Dim PrevVal As Variant
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Column = 2 Then
    
        Application.ScreenUpdating = False
    
        Dim rng As Range
        Dim copyVal As String
    
        Set rng = Nothing
        Set rng = Range("A" & Target.Row & ":B" & Target.Row)
    
        'copy the values
        With Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp)
            .Offset(1, 0).Resize(1, rng.Cells.Count).Value = rng.Value
    
            With Worksheets("Sheet1")
                Range("A" & Target.Row).Copy
                copyVal = CStr(PrevVal)
            End With
    
            .Offset(1, 0).PasteSpecial xlPasteFormats
    
            .Offset(1, 1) = copyVal
            Application.CutCopyMode = False
    
        End With
    End If
    
    Application.ScreenUpdating = True
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Rows.Count > 1 Then Exit Sub
    If Target.Columns.Count > 1 Then Exit Sub
    
    PrevVal = Target.Value
    End Sub
    
    0 讨论(0)
提交回复
热议问题