+ Reply to Thread
Results 1 to 6 of 6

Dynamic Size for Tables Created in VBA

  1. #1
    Registered User
    Join Date
    11-06-2006
    Posts
    15

    Dynamic Size for Tables Created in VBA

    I'm a VBA rookie.

    I have this code from recording. All I'm doing is selecting all data, and making it into a table. Problem is the amount of data, in terms of rows, always changes (columns are constant). How can I make this dynamic so it counts the number of rows and makes the table accordingly.

    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$AC$385"), , xlYes).Name = _
    "Table1"

    Problem is the hard-coded "$AC$538" thanks!

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Dynamic Size for Tables Created in VBA

    Without seeing the Table, try replacing:

    Range("$A$1:$AC$385")
    with:

    Please Login or Register  to view this content.
    Gary's Student

  3. #3
    Registered User
    Join Date
    11-06-2006
    Posts
    15

    Re: Dynamic Size for Tables Created in VBA

    It sort of worked, but this, which I didnt include right below the code now doesnt work:

    ActiveSheet.ListObjects.Add(xlSrcRange, rng, , xlYes).Name = "Table1" (error 1004)

    Thanks for the help

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Dynamic Size for Tables Created in VBA

    Looks like we need to Set rng to something somewhere?

  5. #5
    Registered User
    Join Date
    11-06-2006
    Posts
    15

    Re: Dynamic Size for Tables Created in VBA

    Here is more code if it helps

    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).Name = _
    "Table1"
    Range("Table1[#All]").Select
    ActiveSheet.ListObjects.Add(xlSrcRange, rng, , xlYes).Name = "Table1"
    Range("Table1[[#Headers],[Ask Sz]]").Select
    ActiveCell.FormulaR1C1 = "Ask Sz"
    Range("Table1[[#Headers],[Ask Sz]]").Select

  6. #6
    Registered User
    Join Date
    11-06-2006
    Posts
    15

    Re: Dynamic Size for Tables Created in VBA

    Nevermind, I re-recorded so that line is gone. Thanks for the help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1