How can I define/refer to this range without string concatenation?

RobertSF

Board Regular
Joined
Jun 20, 2017
Messages
104
I have a table (ListObject) with a fixed number of columns and a variable number of rows. The table is a perfect grid except there's a column that is empty for some rows and not for others. I sort the table on that column to separate the rows with values in that column from rows with empty cells in that column. Now I would like to define the range within the table that contains the header and the "complete" rows (i.e. the ones with data in all columns).

Code:
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects(1)
tbl.Sort.SortFields.Clear
tbl.Range.Sort Key1:=Range("MyTable[MyColumn]"), Order1:=xlAscending

Dim numRows As Long
numRows = tbl.ListColumns("MyColumn").Range.Cells.Count - _
          tbl.ListColumns("MyColumn").Range.SpecialCells(xlCellTypeBlanks).Count - 1

' Would be nice if you could do something like this, but you can't:
' Set rng = tbl.Range(rows(1),rows(numRows))

As you can see in the above code, I define the table, sort it, and determine how many rows are complete (numRows). But now how do I define the range that consists of those complete rows?

I want the rows in the table (not rows all across to column IV), but only rows from 1 to whatever is in numRows. How to do this without resorting to string concatenation to build visual addresses like this?

Code:
range("A1:H" & numRows)
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Darn... two minutes more of googling, and I found the answer. It's the resize method.

Code:
Dim tbl As ListObjectSet tbl = ActiveSheet.ListObjects(1)
tbl.Sort.SortFields.Clear
tbl.Range.Sort Key1:=Range("MyTable[MyColumn]"), Order1:=xlAscending


Dim numRows As Long
numRows = tbl.ListColumns("MyColumn").Range.Cells.Count - _
          tbl.ListColumns("MyColumn").Range.SpecialCells(xlCellTypeBlanks).Count - 1


' Would be nice if you could do something like this, but you can't:
' Set rng = tbl.Range(rows(1),rows(numRows))

'Instead do this:
Set rng = tbl.Range.Resize(numRows)

Yeah, I'm a noob. :laugh:
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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