Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Items in a discontiguous range


I would like to know what the next cell is in a discontiguous named
range but am having trouble with the item property of the range.

I pass this function the range and the starting cell


Function GetNextCellInRange(rngTarget As Range, rngTestCell As Range)
As Object

Dim iCount As Integer
Dim oCell As Object

iCount = 1

For Each oCell In rngTarget
If oCell.Row = rngTestCell.Row Then
If oCell.Column = rngTestCell.Column Then ' I've found the my
cell
Exit For
End If
End If
iCount = iCount + 1
Next

' get next cell
iCount = iCount + 1

' If no match is found or is past the end, set return cell as first in
range
If iCount rngTarget.Count Then
iCount = 1
End If

Set GetNextCellInRange = rngTarget.Item(iCount)

End Function


I have a range that consists of two columns - it might consist of more
later.
If I pass the last cell in the first column, or any cell in the second
column, it is found ok but unfortunately rngTarget.Item(icount) refers
to a cell icount rows beneath the start of the first column not the
next cell in the range.

How do I get round this (areas perhaps?). I'd be v. grateful for any
help.

Cheers

Gordon

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Items in a discontiguous range

Hi Gordon,

I would like to know what the next cell is in a discontiguous named
range but am having trouble with the item property of the range.


I haven't looked closely at your function but the normal way is something
like this

Dim rng as Range, rA as Range, rCell as range
For Each rA in rng.Areas
For Each rCell in rA
'code
Next
Next

Regards,
Peter T

wrote in message
ps.com...

I would like to know what the next cell is in a discontiguous named
range but am having trouble with the item property of the range.

I pass this function the range and the starting cell


Function GetNextCellInRange(rngTarget As Range, rngTestCell As Range)
As Object

Dim iCount As Integer
Dim oCell As Object

iCount = 1

For Each oCell In rngTarget
If oCell.Row = rngTestCell.Row Then
If oCell.Column = rngTestCell.Column Then ' I've found the my
cell
Exit For
End If
End If
iCount = iCount + 1
Next

' get next cell
iCount = iCount + 1

' If no match is found or is past the end, set return cell as first in
range
If iCount rngTarget.Count Then
iCount = 1
End If

Set GetNextCellInRange = rngTarget.Item(iCount)

End Function


I have a range that consists of two columns - it might consist of more
later.
If I pass the last cell in the first column, or any cell in the second
column, it is found ok but unfortunately rngTarget.Item(icount) refers
to a cell icount rows beneath the start of the first column not the
next cell in the range.

How do I get round this (areas perhaps?). I'd be v. grateful for any
help.

Cheers

Gordon



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Items in a discontiguous range

GS - Thanks very much, I hadn't thought of letting the For Each...Next
loop go one further. Maybe it was because I was brought up not to use
flags ;-) (I blame that Michael Jackson - well not THAT Michael
Jackson)

Also I'd forgotten (if I'd ever known) about the intersect method -
Thanks.

I still find it curious that the range.item index doesn't work as you'd
expect. There must be an index somewhere as the For Each loop must use
it.

Thanks again

Cheers

Gordon


On Nov 30, 3:33 pm, Gary''s Student
wrote:
This is not an answer to your problem, but an alternate method

Function GetNextCellInRange(rngTarget As Range, rngTestCell As Range) As
String
Dim r As Range, b As Boolean
GetNextCellInRange = ""
b = False
For Each r In rngTarget
If b Then
GetNextCellInRange = r.Address
Exit Function
End If

If Intersect(r, rngTestCell) Is Nothing Then
Else
b = True
End If
Next
End Function

Here I made the function return a string because I tested it on the
worksheet. It makes no assumptions on the column or row structure of
rngTarget
--
Gary's Student

" wrote:

I would like to know what the next cell is in adiscontiguousnamed
range but am having trouble with the item property of the range.


I pass this function the range and the starting cell


Function GetNextCellInRange(rngTarget As Range, rngTestCell As Range)
As Object


Dim iCount As Integer
Dim oCell As Object


iCount = 1


For Each oCell In rngTarget
If oCell.Row = rngTestCell.Row Then
If oCell.Column = rngTestCell.Column Then ' I've found the my
cell
Exit For
End If
End If
iCount = iCount + 1
Next


' get next cell
iCount = iCount + 1


' If no match is found or is past the end, set return cell as first in
range
If iCount rngTarget.Count Then
iCount = 1
End If


Set GetNextCellInRange = rngTarget.Item(iCount)


End Function


I have a range that consists of two columns - it might consist of more
later.
If I pass the last cell in the first column, or any cell in the second
column, it is found ok but unfortunately rngTarget.Item(icount) refers
to a cell icount rows beneath the start of the first column not the
next cell in the range.


How do I get round this (areas perhaps?). I'd be v. grateful for any
help.


Cheers


Gordon


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
Deselect a cell in a discontiguous range Excel_VBA_Newb Excel Discussion (Misc queries) 3 October 8th 09 09:02 PM
Is it possible to plot discontiguous cells? Peter Bernadyne Excel Discussion (Misc queries) 0 May 9th 06 10:32 PM
named range, data validation: list non-selected items, and new added items KR Excel Discussion (Misc queries) 1 June 24th 05 05:21 AM
discontiguous ranges in charts Pilgrim Excel Programming 1 July 3rd 04 05:53 PM
Defining a discontiguous Range object Charley Kyd[_2_] Excel Programming 15 December 30th 03 12:33 AM


All times are GMT +1. The time now is 05:34 PM.

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"