Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Longer loop than expected

Hello,
I always think: oh I can do that quickly in a couple of
minutes and then it seems to take me houres...

So here's what happened this time:
I programmed a small loop but excel seems to loop through
it a more than the possible maximum and somtimes gives
unexpected results.
Maybe it has something to do with formulas:
When the values of the array (range) are not formulas: it
works ok. But if there are formulas...

Function lastvalueinarray(table_array As Range) As Variant
Dim i As Integer
Application.Volatile
i = 1
MsgBox (table_array.Rows.Count)
While i < table_array.Rows.Count
MsgBox (i)
MsgBox (table_array.Item(i, 1).Value)
i = i + 1
Wend
lastvalueinarray = table_array(i, 1).Value
End Function

The messageboxes are there as a sort of debugging.

One should expect that the maximum number of loops
is 'count'. So there would appear 2xcount messageboxes.
But it is a multiple of that. On top of that: If the array
(range) contains #N/A as result of a formula the result is
#Value! instead of the last value in the array.

I know there are easier ways of getting the last value.
But this provides an easy way of showing and explaining
the problem whithout posting 2 pages of code.

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Longer loop than expected

You can get to the last cell in a single area range with something like this:

dim myRng as range
set myRng = range("a1:b99")
msgbox myrng.cells(myrng.cells.count).value

(if it doesn't have an error in it.)

And if the range can contain multiple areas, you can go directly to end of the
last area with something like:

Option Explicit
Function lastValueInArray(table_array As Range) As Variant
Dim res As Variant

With table_array
With .Areas(.Areas.Count)
If IsError(.Cells(.Cells.Count).Value) Then
res = .Cells(.Cells.Count).Text
Else
res = .Cells(.Cells.Count).Value
End If
End With
End With

lastValueInArray = res
End Function

'tested with:
Sub testme()
MsgBox lastValueInArray(Selection)
End Sub

Bart Deschoolmeester wrote:

Hello,
I always think: oh I can do that quickly in a couple of
minutes and then it seems to take me houres...

So here's what happened this time:
I programmed a small loop but excel seems to loop through
it a more than the possible maximum and somtimes gives
unexpected results.
Maybe it has something to do with formulas:
When the values of the array (range) are not formulas: it
works ok. But if there are formulas...

Function lastvalueinarray(table_array As Range) As Variant
Dim i As Integer
Application.Volatile
i = 1
MsgBox (table_array.Rows.Count)
While i < table_array.Rows.Count
MsgBox (i)
MsgBox (table_array.Item(i, 1).Value)
i = i + 1
Wend
lastvalueinarray = table_array(i, 1).Value
End Function

The messageboxes are there as a sort of debugging.

One should expect that the maximum number of loops
is 'count'. So there would appear 2xcount messageboxes.
But it is a multiple of that. On top of that: If the array
(range) contains #N/A as result of a formula the result is
#Value! instead of the last value in the array.

I know there are easier ways of getting the last value.
But this provides an easy way of showing and explaining
the problem whithout posting 2 pages of code.

Thanks


--

Dave Peterson

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Longer loop than expected

Try taking the application.volatile out of your function. I think excel wants
to recalculate each time you change something. And your formulas are putting it
in a loop.


Bart Deschoolmeester wrote:

Oh just leave it: nowbody will read this thread again: so
I'll have to repost:
but please: READ!!! the message you reply to: getting the
last value of a range was not my aim. My aim is that you
would run the code and see what's wrong: why does it loops
the code more than it's apparent maximum (number of rows).
-----Original Message-----
You can get to the last cell in a single area range with

something like this:

dim myRng as range
set myRng = range("a1:b99")
msgbox myrng.cells(myrng.cells.count).value

(if it doesn't have an error in it.)

And if the range can contain multiple areas, you can go

directly to end of the
last area with something like:

Option Explicit
Function lastValueInArray(table_array As Range) As Variant
Dim res As Variant

With table_array
With .Areas(.Areas.Count)
If IsError(.Cells(.Cells.Count).Value) Then
res = .Cells(.Cells.Count).Text
Else
res = .Cells(.Cells.Count).Value
End If
End With
End With

lastValueInArray = res
End Function

'tested with:
Sub testme()
MsgBox lastValueInArray(Selection)
End Sub

Bart Deschoolmeester wrote:

Hello,
I always think: oh I can do that quickly in a couple of
minutes and then it seems to take me houres...

So here's what happened this time:
I programmed a small loop but excel seems to loop

through
it a more than the possible maximum and somtimes gives
unexpected results.
Maybe it has something to do with formulas:
When the values of the array (range) are not formulas:

it
works ok. But if there are formulas...

Function lastvalueinarray(table_array As Range) As

Variant
Dim i As Integer
Application.Volatile
i = 1
MsgBox (table_array.Rows.Count)
While i < table_array.Rows.Count
MsgBox (i)
MsgBox (table_array.Item(i, 1).Value)
i = i + 1
Wend
lastvalueinarray = table_array(i, 1).Value
End Function

The messageboxes are there as a sort of debugging.

One should expect that the maximum number of loops
is 'count'. So there would appear 2xcount messageboxes.
But it is a multiple of that. On top of that: If the

array
(range) contains #N/A as result of a formula the result

is
#Value! instead of the last value in the array.

I know there are easier ways of getting the last value.
But this provides an easy way of showing and explaining
the problem whithout posting 2 pages of code.

Thanks


--

Dave Peterson

.


--

Dave Peterson

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
Too few parameters. Expected 2 Suzanne Excel Discussion (Misc queries) 1 June 5th 08 12:46 AM
How to find the Expected Value of a PDF backformystuff Excel Worksheet Functions 1 February 1st 07 06:54 AM
Not sorting as expected Robbie New Users to Excel 4 August 15th 06 02:27 AM
Expected end of Statement FIRSTROUNDKO via OfficeKB.com Excel Worksheet Functions 2 April 11th 06 11:59 AM
Expected:expression Capinvest Excel Programming 3 August 4th 03 02:36 PM


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

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

About Us

"It's about Microsoft Excel"