VBA to count rows in range and...

gino59

Active Member
Joined
Jul 26, 2010
Messages
496
Hi all,

I have a query which dumps a data range into a worksheet. The data always takes thirteen columns and a variable number of rows. Is there a way to count the number of rows after the query returns the data range and then use that count to specify the size of an Excel 2007 table?

Right now, I'm using the following code to size the table but as you can see, the range is specified using row/column designations.

Code:
          Range("A1").Select
               ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$M$2200"), , xlYes).Name = "tbl1"

It would be good to...
1) write the data to the sheet (got that part done)
2) count the number of rows used in the sheet
3) size the table as A1:Mxxxx where xxxx is the variable number found by counting the range rows.

Many thanks as always!

Cheers,
Gino
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,
There's a couple ways, if you have the range as an object then you can use 'range.rows.count' - allow for the header rows too.
Alternatively you can get the last used row, beware though that this is only the last used row for the selected column.
Range("A65536").End(xlUp).Row
 
Upvote 0
Here's a common mothod for that..

Code:
Dim LR As Long
 
LR = Cells(Rows.Count, "A").End(xlup).Row
Range("A1:M" & LR)

Hope that helps.
 
Upvote 0
Sweet!!!

Thanks all y'all!!!

Auto-sizing tables was driving me nuts!

Many thanks!

Cheers!
Gino
 
Upvote 0
Ha, Jonmo1 I like it especially as it will work for excel 2003 and 2007 with it's non 65,000 row limit.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,111
Members
452,302
Latest member
TaMere

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