Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default how to select two separate ranges into range object

Lets say I have

dim xlRng1 as excel.range

And I would like to set xlRng1 to two separate blocks of data from A1:A5 and
F1:F5

So I tried doing it like this:

set xlRng1 = Range("A1:A5, F1:F5")

so then xlRng1(1,1) should equal to value of A1
but why doesn't xlRng1(1,2) equal to value of F1, but instead it keeps
giving me value of B1 and how do I fix it?

i.e. I would like xlRng1 to consist of two columns A and F, not A through F

Thanks!

Serge


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default how to select two separate ranges into range object

Hi Serge,

set xlRng1 = Range("A1:A5, F1:F5")

so then xlRng1(1,1) should equal to value of A1
but why doesn't xlRng1(1,2) equal to value of F1, but instead it keeps
giving me value of B1 and how do I fix it?


Cells(1,2) is always going to give you the cell to the right of A1 in this
case. Your range xlRng1 actually refers to the correct range, but you can
access F1 differently:

Debug.Print xlRng1.Areas(2).Cells(1, 1).Value

Pretty ugly, but it works. Discontiguous ranges are difficult to deal with
if you need to know the location of each cell. To do it, you could loop
through the values like this to find out which one resides in F1:

Dim c As Range

For Each c In rng.Cells
If c.Address = "$F$1" Then
Debug.Print c.Value
End If
Next c

But I would suggest using 2 Range objects to point to those ranges to make
them easier to work with.

--
Regards,

Jake Marx
www.longhead.com


Serge wrote:
Lets say I have

dim xlRng1 as excel.range

And I would like to set xlRng1 to two separate blocks of data from
A1:A5 and F1:F5

So I tried doing it like this:

set xlRng1 = Range("A1:A5, F1:F5")

so then xlRng1(1,1) should equal to value of A1
but why doesn't xlRng1(1,2) equal to value of F1, but instead it keeps
giving me value of B1 and how do I fix it?

i.e. I would like xlRng1 to consist of two columns A and F, not A
through F

Thanks!

Serge


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default how to select two separate ranges into range object

Thank you Jake! Areas is what I was looking for.

-----Original Message-----
Hi Serge,

set xlRng1 = Range("A1:A5, F1:F5")

so then xlRng1(1,1) should equal to value of A1
but why doesn't xlRng1(1,2) equal to value of F1, but

instead it keeps
giving me value of B1 and how do I fix it?


Cells(1,2) is always going to give you the cell to the

right of A1 in this
case. Your range xlRng1 actually refers to the correct

range, but you can
access F1 differently:

Debug.Print xlRng1.Areas(2).Cells(1, 1).Value

Pretty ugly, but it works. Discontiguous ranges are

difficult to deal with
if you need to know the location of each cell. To do it,

you could loop
through the values like this to find out which one

resides in F1:

Dim c As Range

For Each c In rng.Cells
If c.Address = "$F$1" Then
Debug.Print c.Value
End If
Next c

But I would suggest using 2 Range objects to point to

those ranges to make
them easier to work with.

--
Regards,

Jake Marx
www.longhead.com


Serge wrote:
Lets say I have

dim xlRng1 as excel.range

And I would like to set xlRng1 to two separate blocks

of data from
A1:A5 and F1:F5

So I tried doing it like this:

set xlRng1 = Range("A1:A5, F1:F5")

so then xlRng1(1,1) should equal to value of A1
but why doesn't xlRng1(1,2) equal to value of F1, but

instead it keeps
giving me value of B1 and how do I fix it?

i.e. I would like xlRng1 to consist of two columns A

and F, not A
through F

Thanks!

Serge


.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Average Ranges of Cells, Return Separate Value for Each Range Eric H Excel Worksheet Functions 5 October 19th 09 04:39 PM
Macro to select Range to protect and unprotect ranges [email protected] Excel Worksheet Functions 2 May 15th 09 06:04 AM
Range object to Array object conversion Myrna Larson[_2_] Excel Programming 1 August 1st 03 02:27 AM
Range object to Array object conversion Alan Beban[_3_] Excel Programming 0 August 1st 03 01:24 AM
Range object to Array object conversion Tom Ogilvy Excel Programming 0 August 1st 03 12:16 AM


All times are GMT +1. The time now is 06:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"