View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Increment an Array question

Actually, I think this is an anomaly in the use of isempty.

Isempty checks if a variant variable has been initialized. The purpose of
the check in this code is to see whether varr(1) has been initialized and if
not assign the value to varr(1). If it has, then raise the upperbound and
assign it to the last item in the array. Unfortunately, since varr(1) is
being set as a reference to a range, if it does hold a range, then isempty
checks the situation for the range (is the range empty). So for a range
object, the correct test would be

if varr(1) is nothing then

however, if varr(1) has actually never been initialized (which was the real
purpose of the test) then this test will fail since only an object can be
checked with "is nothing"

To overcome this, I think you should alter your code to:

i = i + 1
If TypeName(varr(1)) = "Empty" Then
Set varr(1) = .Range("B" & StartRw)
Else
ReDim Preserve varr(1 To UBound(varr) + 1)
Set varr(UBound(varr)) = .Range("B" & StartRw)
End If

You can ignore my 2nd response that if a range is assigned it can't
evaluate to empty - I went crazy for a minute <g. Hopefully this will fix
your problem. If not, post back.

--
Regards,
Tom Ogilvy




"S G Booth" wrote in message
...
That was indeed your code, thanks, and it is still working fine for me.
This is a similar situation (re pages), but where the user has used a
routine to insert a page into the sheet.....I now need to revise the
Collection page(s) at the end of the sheet.
I'm trying to find where the first Collection page starts, and if there
are subsequent Collection pages, how many.

As before, each page starts with "Item" in Col1.
If it's the initial Collection page, then an offset from "Item"
Offset(1, 1).End(xlDown).Value should return "COLLECTION".
If it's a subsequent Collection then it'll be "COLLECTION (Cont.)".
Any other value should mean a normal data page.

In my test sheet I had "Item" in A1, A55, A114, A173.
I had "COLLECTION" in B58, and "COLLECTION (Cont.)"
in B117 and B176. To mark the end of the sheet, I used "£" in
F231.

Sub TestArray()
Dim C As Range, TargetCell As String
Dim myRng As Range, £Col As Integer
Dim LastRw As Long, ws As Worksheet
Dim StartRw As Long
Dim varr()

£Col = 6
Set ws = ActiveSheet
With ws
Set myRng = (.Cells(65536, £Col).End(xlUp) _
.Offset(0, -(£Col - 1)))
myRng.Select
LastRw = myRng.Row
ReDim varr(1 To 1)
Do 'Until
TargetCell = .Columns(1).Find(What:="Item", _
After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Select
If ActiveCell.Offset(1, 1).End(xlDown).Value _
= "COLLECTION" Then
'it should mean only one Collection page
StartRw = ActiveCell.Offset(1, 1) _
.End(xlDown).Row + 2
If IsEmpty(varr(1)) Then
Set varr(1) = .Range("B" & StartRw)
Else
ReDim Preserve varr(1 To UBound(varr) + 1)
Set varr(UBound(varr)) = .Range("B" & StartRw)
End If
ElseIf ActiveCell.Offset(1, 1).End(xlDown).Value = _
"COLLECTION (Cont.)" Then
'it should mean multiple Collections
StartRw = ActiveCell.Offset(1, 1) _
.End(xlDown).Row + 2
If IsEmpty(varr(1)) Then
Set varr(1) = .Range("B" & StartRw)
Else
ReDim Preserve varr(1 To UBound(varr) + 1)
Set varr(UBound(varr)) = .Range("B" & StartRw)
End If
ElseIf ActiveCell.Row = 1 Then
Exit Do
End If
Loop
End With
End Sub

For me it seems to overwrite varr(1) rather than increment the
array.....and I don't know why.

Any help much appreciated.

Regards.

"Tom Ogilvy" wrote in message
...
by the way, my explanation was flawed. Since it is a set command, once
used, varr(1) can not be empty and will not be overwritten (at least

with
the code you show - if you empty it somewhere else, then there is no
accounting for that).

as I recall, you were trying to identify pagebreaks, so apparently code
that
you don't show prevents assignments as you expect. the code you show
would
work as designed.

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...
didn't I write the original quite a while ago? Such as



http://groups.google.co.uk/groups?hl...TK2MSF TNGP12

or is it derived from something I wrote?

Has it not worked for a long time or has it always been problematic?

--
Regards,
Tom Ogilvy



"S G Booth" wrote in message
...
Have seen both your replies and Jim's. Thanks to you both. It is a

segment
in a bigger series of routines.
I still cannot see the error(s).

Will try to create a simple set of data, with the relevent
code routine and post back.

Regards and thanks.

"Tom Ogilvy" wrote in message
...
If can do two things
if the first element is empty, it writes the range value in the
first
element.

otherwise, it increases the upperbound of the array by one and

writes
it
in
the new element.


I am not sure what role "i" plays, but you increment that and don't

use
it.

If StartRw is not being incremented, then you would write the same

value
in
every element.

I find that the following code is overwriting varr(1) each time

Not unless .Range("B" & StartRw) is empty and you are placing an
empty
value
in varr(1)

--
Regards,
Tom Ogilvy



"S G Booth" wrote in message
...
I'm searching up a column looking for a particular value.
When found, I need to add a range offset from that cell
to an array.
I find that the following code is overwriting varr(1) each
time....so the array never builds correctly
i = i + 1
If IsEmpty(varr(1)) Then
Set varr(1) = .Range("B" & StartRw)
Else
ReDim Preserve varr(1 To UBound(varr) + 1)
Set varr(UBound(varr)) = .Range("B" & StartRw)
End If

Why is the array not building correctly, please?

Regards