+ Reply to Thread
Results 1 to 12 of 12

How to refer to Table inside a sheet using ListObjects

  1. #1
    Registered User
    Join Date
    11-16-2017
    Location
    MANILA
    MS-Off Ver
    365
    Posts
    11

    How to refer to Table inside a sheet using ListObjects

    Hello to all, very new to vba and starting to learn macros.

    I have macros that are saved inside each sheet in my workbook via Worksheet_Calculate()

    I was able to refer to workbook where the code is save using "ThisWorkbook"
    Also to the sheet where the code is save using "Sheets(Me.Name)"

    Q1 - How can I refer to the table where the code is save? I have about 90 sheets in my workbook and each sheet has 1 table inside.

    Q2 - Once I identified the table, How can I auto resize it using CountifA function I put in cell B1. My range starts with cell A5 and end at cell J(value of cell B1).

    Kindly refer to the code below. Thanks.

    Private Sub Worksheet_Calculate()

    Dim wks As Worksheet

    Set wks = ThisWorkbook.Sheets(Me.Name)

    wks.ListObjects("Table Name").Resize Range("$A$5:$J(value of cell B1)")

    End Sub

  2. #2
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: How to refer to Table inside a sheet using ListObjects

    if there is only one table per worksheet, then do the following

    Please Login or Register  to view this content.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to refer to Table inside a sheet using ListObjects

    If you are using worksheet event code then you can refer to the table on each worksheet, in that worksheet's module, using Me.ListObjects(1).

    Note, that's just Me not Sheets(Me.Name) - Me refers directly to the worksheet the code is in, so you don't need Sheets(...).
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    11-16-2017
    Location
    MANILA
    MS-Off Ver
    365
    Posts
    11

    Re: How to refer to Table inside a sheet using ListObjects

    Thanks dmcgov! The code worked for me since I only have one table per worksheet.

    Here is what my code looked like...

    Private Sub Worksheet_Calculate()

    Dim wks As Worksheet

    Set wks = ThisWorkbook.Sheets(Me.Name)

    wks.ListObjects(1).Resize Range("A5:J" & Range("A1").Value2)

    End Sub

  5. #5
    Registered User
    Join Date
    11-16-2017
    Location
    MANILA
    MS-Off Ver
    365
    Posts
    11

    Re: How to refer to Table inside a sheet using ListObjects

    Hi Norie, been trying to make the code work but to no avail...can you please write the complete lines of codes.
    Thank you.

    By the way this code worked for me...

    Private Sub Worksheet_Calculate()

    Dim wks As Worksheet

    Set wks = ThisWorkbook.Sheets(Me.Name)

    wks.ListObjects(1).Resize Range("A5:J" & Range("A1").Value2)

    End Sub

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to refer to Table inside a sheet using ListObjects

    What Norie means is this
    Please Login or Register  to view this content.
    you can in fact omit the "me." part completely
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  7. #7
    Registered User
    Join Date
    11-16-2017
    Location
    MANILA
    MS-Off Ver
    365
    Posts
    11

    Re: How to refer to Table inside a sheet using ListObjects

    Tried it...using Me.ListObjects(1)... and just ListObject(1)...
    There is an error that says
    Run-time error ‘1004’:
    Application-defined or object-defined error
    ...maybe because the code should be running inside the sheet even if it is not the active workbook or sheet


    Quote Originally Posted by xlnitwit View Post
    What Norie means is this
    Please Login or Register  to view this content.
    you can in fact omit the "me." part completely

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to refer to Table inside a sheet using ListObjects

    Where is the code located?

    Also, what's the value in A1?

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to refer to Table inside a sheet using ListObjects

    If the code you posted in #5 works, so should the version in #6 since they refer to the same sheet.

  10. #10
    Registered User
    Join Date
    11-16-2017
    Location
    MANILA
    MS-Off Ver
    365
    Posts
    11

    Re: How to refer to Table inside a sheet using ListObjects

    Quote Originally Posted by Norie View Post
    Where is the code located?

    Also, what's the value in A1?

    Code is located in each sheet ...via private sub worksheet_calculate()
    A1 is =IF(ISNA(B1),6,B1+6)
    B1 is =OFFSET([DailySettings.xlsm]INFO!$A$6,M1,1) ....from another sheet
    INFO!$A$6 =COUNTIF(MAIN!E:E,A7)

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to refer to Table inside a sheet using ListObjects

    Where in the sheets are the tables located?

  12. #12
    Registered User
    Join Date
    11-16-2017
    Location
    MANILA
    MS-Off Ver
    365
    Posts
    11

    Re: How to refer to Table inside a sheet using ListObjects

    Quote Originally Posted by Norie View Post
    Where in the sheets are the tables located?
    Table is located in each sheet, from cell A5 to cell J(value of A1)

    maybe you can suggest another solution because putting the dynamic range of the table inside worksheet_calculate make the entire workbook run so slow because any change in the other workbook (DailySettings.xlsm) will make this workbook (DailyLogs.xlms - where the codes per sheet is located) update and it takes to long....have to force quit at times.
    I just want to set the range of my table to auto expand based on the count in the other workbook (DailySettings.xlsm) without macro....but excel doesn't seem to allow me to do that . The formula inside the named range for table is grayed out.

    Maybe you can just give me a code that i can manually run in the other workbook (DailySettings.xlsm) to update each sheets' table range (each sheet in DailyLogs.xlsm) based on the current count for the day. The reference will be the name of the cell in the other workbook (DailySettings.xlsm) is equal to the name of the sheet in this workbook (DailyLogs.xlsm).
    any help/idea will be appreciated. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] delete entire active cells row/s only if thet all inside of a table (whatever table it is)
    By netanel99 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-02-2017, 01:42 PM
  2. Replies: 2
    Last Post: 02-29-2016, 09:37 PM
  3. Me.ListObjects("tblName").DataBodyRange.Rows.Delete - Does Me Not Like ListObjects?
    By snapfade in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-30-2014, 01:56 PM
  4. Replies: 7
    Last Post: 12-16-2012, 04:24 PM
  5. Replies: 2
    Last Post: 03-27-2011, 10:05 AM
  6. [SOLVED] How do I link a cell outside a pivot table to one inside the table
    By GPGTDRVR in forum Excel General
    Replies: 3
    Last Post: 08-16-2006, 09:50 PM
  7. [SOLVED] How do I refer to other macros inside a macro code?
    By Erkka in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2005, 11:05 AM

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