View Full Version : Solved: Build table in 2010

06-07-2011, 01:11 AM

I recorded the following macro in excel 2010....

ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$F$87"), , xlYes).Name = _

but now realise that the size changes each day. Please could some one help with code to build a table with a changing number of rows

many thanks

06-07-2011, 02:04 AM
is it

ActiveSheet.ListObjects.Add(xlSrcRange, Range("tbl"), , xlYes).Name = "Table1"

tbl is defined range create via following formula:


data is the worksheet name that contains table data

06-07-2011, 04:21 AM
Thanks for reply but unfortunately don't understand.

I assume you replace the 1st bit of code with what was recorded but
how is the 'data' bit stored and where?


Kenneth Hobs
06-07-2011, 05:04 AM
Do the 2nd part first.

tbl is defined range create via following formula This means that when you create the named range tbl, then add the following formula that was posted. This is called a dynamic named range.

data is the worksheet name that contains table data Data is the worksheet name. Typically, that might be Sheet1.

06-07-2011, 05:08 AM
Thanks for reply - but add the formula where?


06-07-2011, 05:12 AM
"data" is a sample worksheet name where your table is and should be changed to your actual worksheet name.

Named Range:
ribbon, formulas, define name, define name (or ribbon, formulas, name manager, new)
name: tbl (or whatever you like)
scope: (up to you, workbook or...)
refers to: =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),COUNTA(Sheet1!$1:$1))
($A$1 means range's (or table's) top left cell is A1)

by a dynamic range any added columns and rows are covered without havig to change the range in the code or formulas.

actually, if it is an xl Table, new rows and columns are added to the Table automatically.

Kenneth Hobs
06-07-2011, 05:15 AM
Add the formula in the Refers To: in 2010's Formulas > Name Manager > New > tbl > in Refers to: =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),COUNTA(Sheet1!$1:$1)) > OK.

06-07-2011, 06:17 AM
Thanks for reply,

Not sure I can do this

The macro(s) are held in a separate spreadsheet - first thing it does is pick up the new days text file (which has all the numbers etc) and changes it in to a spreadsheet. I can rename the text file to 'data' so that when it creates the spreadsheet its called 'data' (and change the code so it picks the text file called data.

But I can't make the changes to the sheet called 'data' everytime - can I do all the fancy stuff in the code by using the macro recorder?


Kenneth Hobs
06-07-2011, 07:43 AM
ActiveSheet.ListObjects.Add(xlSrcRange, _
Range("A1:F" & Range("F" & Rows.Count).End(xlUp).Row), _
, xlYes).Name = "Table1"

06-08-2011, 07:09 AM
Thanks Kenneth,

Just what I was looking for - worls like a charm