+ Reply to Thread
Results 1 to 4 of 4

How to Count Number of Used Rows in a Specified Column in a Table

  1. #1
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Question How to Count Number of Used Rows in a Specified Column in a Table

    Hello all,

    I currently am writing a macro that is supposed to count the number of used rows in a table for several different tables and adds them up to determine the size of another table which will consolidate the individual tables' data. The code currently looks like this:

    Dim i, TotalRows as Long

    TotalRows = 0

    for i = 1 to ActiveSheet.ListObjects.Count

    TotalRows = ActiveSheet.Listobjects(i).DataBodyRange.Rows.Count + TotalRows

    next i

    Here is the issue - the tables contain several extra empty rows for users to input data into. There is a formula in one column of the table that autofills to all rows, including any rows that are left empty. These empty rows need to remain in the table in case users need to add additional data later. Because of this formula, the .DataBodyRange property is returning all rows including the ones the user leaves blank. When I consolidate the data in the tables into one table, it would then include these blank rows.

    Is there any way I can point directly towards a desired column's used range - for instance, one that the user will always have to manually fill? My thinking is something like this, but it doesn't work as intended:

    TotalRows = Worksheet.Listobjects(i).ListColumns(1).DataBodyRange.Rows.Count + ResourcesRows

    I am aware that I can loop through the values in the tables' desired column individually until I encounter an empty cell, but I am wondering if there is a cleaner approach that will utilize the table's properties. Any help is greatly appreciated.

  2. #2
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: How to Count Number of Used Rows in a Specified Column in a Table

    Hi. For example:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: How to Count Number of Used Rows in a Specified Column in a Table

    I did a bit more Googling and came up with the following solution:

    Dim i, TotalRows as Long

    TotalRows = 0

    for i = 1 to ActiveSheet.ListObjects.Count

    TotalRows = Application.WorksheetFunction.CountA(ActiveSheet.Listobjects(i).ListColumns(1).DataBodyRange) + TotalRows

    next i

    Please let me know of any better alternatives that you are aware of, I'd be happy to learn more solutions.

  4. #4
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: How to Count Number of Used Rows in a Specified Column in a Table

    You beat me to it by a minute! Thank you kindly!

+ 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] How to count the number of rows in pivot table sub-row fields by VBA
    By klwong in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-01-2016, 12:51 AM
  2. Replies: 7
    Last Post: 12-05-2014, 09:33 AM
  3. [SOLVED] Count the number of rows of a certain column that have the same value
    By aviatecar in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-28-2014, 04:05 PM
  4. Replies: 1
    Last Post: 05-16-2014, 07:34 PM
  5. [SOLVED] Filtering macro, count number of rows and include count in last column.
    By Folshot in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-12-2012, 07:17 AM
  6. Count number of rows with data in a column....
    By MAButler in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-06-2011, 02:31 PM
  7. Count the number of rows where more than one column meets set crit
    By BarrieVoice in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-14-2006, 02:30 PM

Tags for this Thread

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