ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Describing a Range with Variables (https://www.excelbanter.com/excel-programming/360919-describing-range-variables.html)

mazo

Describing a Range with Variables
 
Hi

I have the following problem:
I want to select a certain Range depending on the value of a variable

e.g.
I habe a variable called nr (current value = 1)
I want to replace:

Range("O1:O200").Select

with:

" Range [ Cells(1,(14+nr)) to Cells(200,(14+nr)) ]"

How can you wright that in VBA?

Mazo


Norman Jones

Describing a Range with Variables
 
Hi Mazo,

You are almost there! Try:

Dim rng As Range

Set rng = Range(Cells(1, 14 + i), Cells(200, 14 + i))


---
Regards,
Norman



"mazo" wrote in message
oups.com...
Hi

I have the following problem:
I want to select a certain Range depending on the value of a variable

e.g.
I habe a variable called nr (current value = 1)
I want to replace:

Range("O1:O200").Select

with:

" Range [ Cells(1,(14+nr)) to Cells(200,(14+nr)) ]"

How can you wright that in VBA?

Mazo




Dave Peterson

Describing a Range with Variables
 
Another way:

Dim myRng as range
dim nr as long
nr = 1 'or something

set myrng = activesheet.cells(1,14+nr).resize(1,200)

..resize(1,200) means to take the original range (cells(1,14+nr) and resize it to
1 row by 200 columns.





mazo wrote:

Hi

I have the following problem:
I want to select a certain Range depending on the value of a variable

e.g.
I habe a variable called nr (current value = 1)
I want to replace:

Range("O1:O200").Select

with:

" Range [ Cells(1,(14+nr)) to Cells(200,(14+nr)) ]"

How can you wright that in VBA?

Mazo


--

Dave Peterson


All times are GMT +1. The time now is 08:58 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com