PDA

View Full Version : Solved: Why using Range() works but Cells() doesn't??



nathan2314
11-02-2010, 09:57 AM
Hi,

I need to select a range but needed to define a dynamic variable as things change. My variable is call endcol_d and it is the column I need to put in the range.
So I have

basebook.Sheets("Protect Population").Range(Cells(5, endcol_d), Cells(5, endcol_d + 1)).Select

But that doesn't work. It gives and error. But when I test it using Range("") for one instance it works. basebook.Sheets("Protect Population").Range("B5:C5").Select
How can I make it work using Cells??

Appreciate any help!!

xld
11-02-2010, 11:43 AM
You need to dot qualify everything



With basebook.Sheets("Protect Population")

.Range(.Cells(5, endcol_d), .Cells(5, endcol_d + 1)).Select
End With

nathan2314
11-02-2010, 01:57 PM
Ah that fixed it! Thanks!!
:clap:

kemas
11-13-2010, 06:58 AM
endcol_d = 4
ThisWorkbook.Sheets(1).Range(Cells(5, endcol_d), Cells(5, endcol_d + 1)).Select

mdmackillop
11-13-2010, 08:25 AM
You can also use Resize
basebook.Sheets("Protect Population").Cells(5, endcol_d).Resize(,2).Select