简体   繁体   中英

Copy Multiple Ranges and Paste onto other worksheet next empty row?

I am trying to copy multiple ranges when a user clicks on a cell. I then want to paste these values onto another worksheet on the next available row.

Here's what i've tried so far:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$AB$12:$AD$13" Then
        Range("B1").Copy
        Range("E11").Copy
        Sheets("Data").Range("B" & lastrow).PasteSpecial xlPasteValues
    End If
End Sub

But I get an application undefined error.

I am always a little leary when someone wants to trigger an event on nothing more that a change in cell selection but try this.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("AB12:AD13")) Is Nothing Then
        Worksheets("Data").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Resize(1, 2) = _
            Array(Range("B1").Value, Range("E11").Value)
    End If
End Sub

You might also want to look into the Worksheet.BeforeDoubleClick event as an alternate way of triggering a transfer of values.

try this

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    lastrow = Worksheets("data").Range("B" & Rows.Count).End(xlUp).Row + 1
    If Not Intersect(Target, Range("AB12:AD13")) Is Nothing Then
        Range("B1").Copy Sheets("Data").Range("B" & lastrow)
        Range("E11").Copy Sheets("Data").Range("C" & lastrow)
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM