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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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. :LOL:
 
Upvote 0

Forum statistics

Threads
1,215,963
Messages
6,127,948
Members
449,412
Latest member
montand

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