Forum Discussion
Eggselling
Dec 31, 2022Copper Contributor
Using Macros to add to a table
Keyboard Shortcut: Ctrl+y
'
Dim ws As Worksheet
Set ws = ActiveSheet
Table_Name = InputBox("Table2")
Dm tbl As ListObject
Set tbl = ws.ListObjects(Table_Name)
tbl.ListRows.Add
Range(Selecti...
JKPieterse
Jan 10, 2023Silver Contributor
Eggselling I think this is what you are looking for?
Option Explicit
Sub InsertIntoTable()
Dim ws As Worksheet
Set ws = ActiveSheet
Table_Name = InputBox("Table2")
Dim tbl As ListObject
Set tbl = ws.ListObjects(Table_Name)
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearFormats
Range("A5").Select
Selection.Delete Shift:=xlUp
Range("A6").Select
Selection.Delete Shift:=xlUp
Range("A7").Select
Selection.Delete Shift:=xlUp
Range("A8").Select
Selection.Delete Shift:=xlUp
Range("A4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'This pastes in the left-most cell, immmediately below the last row of the table
'Excel then automatically expands the table with the # of rows you pasted
tbl.ListRows(tbl.ListRows.Count).Range.Offset(1).Resize(1, 1).Paste
End Sub