How to count rows in a table with a user defined column

Graebeard

New Member
Joined
Mar 21, 2015
Messages
23
Office Version
  1. 2019
Platform
  1. Windows
Hi folks,

I have a dynamic table to track activities. There are a bout a dozen fields for each record and not all are always filled in. One column titled "Activity" is always filled in, BUT, since we may add or delete columns, the Activity field is not always in Column G.

I'm writing a VBA macro for the Sort, so I need to know which column to use to count the number of records to be sorted. The field names and their columns are stored in an array called myTitles(x,1) and myTitles(x,2).

I was trying something like noRecords = Counta((myTitles(x,2):myTitles(x,2)) which is similar to the cell formula CountA(G:G)

Obviously it did not work. Any help would be appreciated

TIA

Graebeard
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Counta is a worksheet function that requires range arguments. Why not use the Range.Find method to find "Activity" then use the found ranges column number like so:
Code:
Dim R as range, x as Long
set R = cells.Find("Activity")
if R is Nothing then Exit sub
x = R.Column
noRecords = Application.Counta(Columns(x))
'rest of code
 
Last edited:
Upvote 0
Counta is a worksheet function that requires range arguments. Why not use the Range.Find method to find "Activity" then use the found ranges column number like so:
Code:
Dim R as range, x as Long
set R = cells.Find("Activity")
if R is Nothing then Exit sub
x = R.Column
noRecords = Application.Counta(Columns(x))
'rest of code

Thanks for the quick reply JoeMo.
I've been using Excel since version 3 (I think that was around the mid '80s or so), and writing VBA macros for the past 25 years or so, but I'm always finding something new. The cells.find, the r.column and application.counta(columns(x)) are completely new to me.

So, many thanks. With a little tweaking, it works great.

Graebeard
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top