Page 1 of 1 1
Topic Options
#180892 - 2007-09-27 05:36 AM Problem indexing Variant Arrays
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I'm having an odd problem reading data from Excel. The example below illustrates the problem.

When reading a single cell, it returns an appropriate value. However, when reading a range of cells, it returns a Variant Array. This is where the problem appears. Actually, there are 2 problems...

1. You can look at the UBound of the array, but it isn't "right". If you read one row of cells, the UBound is 1, not 0. If you read a block of cells, UBound defines the number of rows as a 1-based value.

2. You can enumerate the array in a For/Each loop, but you cannot directly address an element of the array.

Both of these problems are illustrated by the code sample below. Both arrays are fully enumerated by For/Each loops, but the last line, which references element 0 of the array will throw an error.

I've tested this with every version since 4.50, including 4.60rc1, and have found the same results. Shawn has also confirmed these findings.

Glenn

 Code:
Break On 

'Kix Version: ' @Kix ?

; Instatiate the Excel object and open the file
; The spreadsheet contains the following simple data:
;	A	B	C	D
;	E	F	G	H
;	I	J	K	L
;	M	N	O	P
$Excel = CreateObject("Excel.Application")
$Workbook = $Excel.Workbooks.Open("c:\temp\test.xls")

'Single Cell:' ?
$Value = $Workbook.ActiveSheet.Range("B2").Value  
'Elements: ' UBound($Value) ?
'Vartype=' VarTypeName($Value) ?
$Value ?
?
'Row of 4 Cells:' ?
$Value = $Workbook.ActiveSheet.Range("A1:D1").Value  
'Elements: ' UBound($Value) ?
'Vartype=' VarTypeName($Value) ?
For Each $ in $Value
 $ ?
Next
?
'Block of 4x4 Cells:' ?
$Value = $Workbook.ActiveSheet.Range("A1:D4").Value
; Close Excel now, since the last command will cause Kix to fail
; which will leave Excel running
$Excel.Quit 
$Excel = 0 
'Elements: ' UBound($Value) ?
'Vartype=' VarTypeName($Value) ?
For Each $ in $Value
 $ ?
Next
$Value[0] ?

Exit 0 
_________________________
Actually I am a Rocket Scientist! \:D

Top
#180934 - 2007-09-28 05:30 AM Re: Problem indexing Variant Arrays [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
this looks like normal kixtart com behavior, without giving it much thought.
don't remember did Ruud promise some change to this in the future or not, but it has been discussed before too.

again, did not run your code, just read it and told what it smells like.
_________________________
!

download KiXnet

Top
#180935 - 2007-09-28 06:15 AM Re: Problem indexing Variant Arrays [Re: Lonkero]
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Earlier today, after Glenn posted this, I ran this code with VB script and it appears to behave the same (can't index into variant array) ... its still odd behavior given that Kixtarts' builtin arrays show as variant arrays also. Would really like to get Ruuds comments on this at least.
Top
#180949 - 2007-09-28 11:45 AM Re: Problem indexing Variant Arrays [Re: Shawn]
Ruud van Velsen Moderator Offline
Developer
*****

Registered: 1999-05-06
Posts: 391
Loc: Amsterdam, The Netherlands
Thanks for the report, I will investigate.

Ruud

Top
Page 1 of 1 1


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1447 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.112 seconds in which 0.077 seconds were spent on a total of 13 queries. Zlib compression enabled.