Home > Macros > Basics > Excel Tables Copy Selected Items in Excel TableUse these macros to copy selected items in an Excel table, and paste at the end, or below the last selected item. Get the free workbook to test the code. |
These macros will copy the selected items in an Excel table, and then:
Before you run either macro:
Additional Notes:
This macro copies the selected items to the end of the Excel table.
Read the notes in the Introduction, for tips on using this macro.
Sub CopySelectionVisibleRowsEnd() Dim ws As Worksheet Dim mySel As Range Dim lRow As Long Dim lRowNew As Long Dim lRowsAdd As Long Dim myList As ListObject Dim myListRows As Long Dim myListCols As Long Set ws = ActiveSheet Set mySel = Selection.EntireRow Set myList = ActiveCell.ListObject myListRows = myList.Range.Rows.Count myListCols = myList.Range.Columns.Count lRow = ws.Cells.Find(What:="*", _ SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, _ LookIn:=xlValues).Row + 1 mySel.SpecialCells(xlCellTypeVisible).Copy ws.Cells(lRow, 1).PasteSpecial Paste:=xlPasteAll lRowNew = ws.Cells.Find(What:="*", _ SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, _ LookIn:=xlValues).Row + 1 lRowsAdd = lRowNew - lRow With myList .Resize ws.Range(.Range.Resize(myListRows + lRowsAdd, myListCols).Address) End With Application.CutCopyMode = False End Sub
Before running the code, select one cell in each row that you want to copy. The macro will copy and paste the entire row for each cell that is selected.
This macro copies the selected items to the row below the last selected item. New rows are inserted there, and the items are pasted into the new rows.
Read the notes in the Introduction, for tips on using this macro.
Sub CopySelectionVisibleRowsInsert() Dim ws As Worksheet Dim mySel As Range Dim mySelVis As Range Dim rngArea As Range Dim lRowSel As Long Dim lRowNew As Long Dim lRowsAdd As Long Dim myList As ListObject Dim myListRows As Long Dim myListCols As Long Dim c As Range Dim lAreas As Long Dim lRowsArea As Long Set ws = ActiveSheet Set mySel = Selection.EntireRow lAreas = Selection.Areas.Count lRowsArea = Selection.Areas(lAreas).Rows.Count Set mySelVis = mySel.SpecialCells(xlCellTypeVisible) lRowSel = mySel.Areas(lAreas).Cells(lRowsArea, 1).Row + 1 For Each rngArea In mySel.Areas For Each c In rngArea.Columns(1).Cells If Not Intersect(c, mySelVis) Is Nothing Then ws.Cells(lRowSel, 1).EntireRow.Insert End If Next c Next rngArea mySel.SpecialCells(xlCellTypeVisible).Copy ws.Cells(lRowSel, 1).PasteSpecial Paste:=xlPasteAll Application.CutCopyMode = False End Sub
Before running the code, select one cell in each row that you want to copy. The macro will copy and paste the entire row for each cell that is selected.
Download the sample workbook with the two macros to copy and paste selected items.
The zipped file is in xlsm format, and it contains macros.
Excel VBA Edit Your Recorded Macro
Last updated: January 24, 2023 7:35 PM