Consulting

Results 1 to 10 of 10

Thread: Build table in 2010

  1. #1

    Build table in 2010

    Hi,

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

    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$F$87"), , xlYes).Name = _
    "Table1"
    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
    Jon

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    is it

        ActiveSheet.ListObjects.Add(xlSrcRange, Range("tbl"), , xlYes).Name =  "Table1"
    tbl is defined range create via following formula:
     
    =OFFSET(data!$A$1,0,0,COUNTA(data!$A:$A),COUNTA(data!$1:$1))
    data is the worksheet name that contains table data
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    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?

    regards
    Jon

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  5. #5
    Thanks for reply - but add the formula where?

    regards

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    "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.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  8. #8
    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?

    cheers

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
      ActiveSheet.ListObjects.Add(xlSrcRange, _
        Range("A1:F" & Range("F" & Rows.Count).End(xlUp).Row), _
        , xlYes).Name = "Table1"

  10. #10
    Thanks Kenneth,

    Just what I was looking for - worls like a charm

    regards
    Jon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •