private sub worksheet_change(byval target as range)
if not intersect(target, range("a2")) is nothing or not intersect(target, range("b2")) is nothing then
application.enableevents = false
dim rng as range
dim rcount as long
dim ccount as long
dim otable as range
rcount = range("a2") + 1
ccount = range("b2")
if activesheet.listobjects("table").showtotals then
activesheet.listobjects("table").totalsrowrange.delete
end if
set otable = range("table" & "[#all]")
'note that the row count includes the header so if you enter 10 you will have 9 rows in the data area and the header row makes 10
'if you want you can add one to the row count rcount = range("l2")+1 so that it gives you that number of rows in the data area
'this just resizes the table the data will still be there.
Set rng = range("table" & "[#all]").resize(rcount, ccount)
activesheet.listobjects("table").resize rng
if otable.rows.count > rng.rows.count then
range(cells(rng.row + rng.rows.count, rng.column), cells(otable.row + otable.rows.count, rng.column + rng.columns.count)).clearcontents
end if
if otable.columns.count > rng.columns.count then
range(cells(rng.row, rng.column + rng.columns.count), cells(otable.row + otable.rows.count, otable.column + otable.columns.count)).clearcontents
end if
activesheet.listobjects("table").showtotals = true
with activesheet.listobjects("table")
.listcolumns("headerb").totalscalculation = xltotalscalculationsum 'change headerb to your column header.
'repeate the above with other column headers and change the calculations as need for example xltotalscalculationcount to count
end with
application.enableevents = true
end if
end sub