Tip Printed from ExcelTip.com
Copy Selection to a Database sheet using VBA in Microsoft Excel

VBA macro tip contributed by Ron de Bruin, Microsoft MVP - Excel



You can use this also in a selection with one Area
Sub CopySelection()
    Dim destrange As Range
    Dim smallrng As Range
    For Each smallrng In Selection.Areas
        Set destrange = Sheets("Sheet2").Range("A" & _
        LastRow(Sheets("Sheet2")) + 1)
        smallrng.Copy destrange
    Next smallrng
End Sub

Sub CopySelectionValues()
    Dim destrange As Range
    Dim smallrng As Range
    For Each smallrng In Selection.Areas
        With smallrng
            Set destrange = Sheets("Sheet2").Range("A" & _
            LastRow(Sheets("Sheet2")) + 1). _
            Resize(.Rows.Count, .Columns.Count)
        End With
        destrange.Value = smallrng.Value
    Next smallrng
End Sub

Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function

Function Lastcol(sh As Worksheet)
    On Error Resume Next
    Lastcol = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
    On Error GoTo 0
End Function