Setting Range by code

  • Thread starter Thread starter Ashanti
  • Start date Start date
A

Ashanti

I have a table which will always have 3 columns but
anything from 2 to 2000 rows. The first row is the column
titles.

Is it possible using code to name a range to all cells in
the table with data in them.

For eg: Table has 250 Lines so range would be A2:C250

Thanks so much
 
Hi
try
sub foo
dim rng as range
dim lastrow as long

LastRow = ActiveSheet.Cells(Rows.count, rng.Column).End(xlUp).row
set rng = range(cells(2,1),cells(lastrow,3))
rng.select
end sub
 
Hi Ashanti

you don't need VBA to create this - its known as a "dyanamic range" and can
be created following the instructions at Debra Dalgleish's (excellent)
website
http://www.contextures.com/xlNames01.html#Dynamic

the formula that you will end up with in your case will be similar to
=OFFSET(Sheet1!$A$2,0,0,Counta(Sheet1!$A:$A),3)

Cheers
JulieD
 
Hi Frank

When i try to run this i get the error message 'Object
block or with variable not set'

Do you know why this may happen?
 
Hi
sorry a copy+paste error. Try

Sub foo()
Dim rng As Range
Dim lastrow As Long

lastrow = ActiveSheet.Cells(Rows.count, "A").End(xlUp).row
Set rng = Range(Cells(2, 1), Cells(lastrow, 3))
rng.Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top