Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Variant as matrix

Some relatively easy question:
Suppose I pass a matrix from spreadsheet to VBA as Variant


Code
-------------------

Public Function foo(matrix As Variant)

End Function

-------------------


How do I get the dimensions of the matrix?
(i.e. what is the equivalent of LBound,UBound for this case)

How do I address the elements of this matrix
(i.e. what is the equivalent of matrix(i,j) for this case)

Thanks a lot

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default Variant as matrix

The easiest way to address every element without knowing its rank (how many dimensions it has) of any matrix is

For each Element in MyMatri
... your cod
Nex

This would not be suitable if you want to reassign to MyMatrix

LBound(MyMatrix) gives the starting index of a 1-dimensional matrix; UBouund the last
LBound(mymatrix,1) dives the starting index on the first dimension etc. Any reference to a non-existent dimension is an error

There is no way to query the number of dimensions in a matrix

Depending on how the variant matrix is created, its starting index on any dimension is 0 when created using Split or Array, irrespective of Option Base


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Variant as matrix

Sub Main()
Dim varr as Variant
varr = Range("A1:A4").Value

foo varr
End Sub

Public Function foo(matrix As Variant)
Dim lb1 as Long, lb2 as Long, ub1 as Long, ub2 as Long
Dim numDim as Long, i as long, j as long
If IsArray(matrix) Then
numDim = 1
lb1 = LBound(matrix, 1)
ub1 = UBound(matrix, 1)
On Error Resume Next
lb2 = LBound(matrix, 2)
ub2 = UBound(matrix, 2)
If Err.Number = 0 Then
Err.Clear
numDim = 2
End If
On Error GoTo 0
If numDim = 1 Then
For i = lb1 To ub1
Debug.Print "i= " & matrix(i)
Next
Else
For i = lb1 To ub1
For j = lb2 To ub2
Debug.Print "Matrix(" & i & ", " & j & ")= " & matrix(i, j)
Next
Next
End If
End If
End Function


--
Regards,
Tom Ogilvy

"curious " wrote in message
...
Some relatively easy question:
Suppose I pass a matrix from spreadsheet to VBA as Variant


Code:
--------------------

Public Function foo(matrix As Variant)

End Function

--------------------


How do I get the dimensions of the matrix?
(i.e. what is the equivalent of LBound,UBound for this case)

How do I address the elements of this matrix
(i.e. what is the equivalent of matrix(i,j) for this case)

Thanks a lot,


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Variant as matrix

Hi
Essentially the same way.
Your Variant matrix will be 1 based, so if you are looping through its
elements start at 1 not 0. Note that a 1 dimensional Variant (a piece
of row or column) will still need two indices e.g. (1, j) if it is a
row

cheers
Paul
curious wrote in message ...
Some relatively easy question:
Suppose I pass a matrix from spreadsheet to VBA as Variant


Code:
--------------------

Public Function foo(matrix As Variant)

End Function

--------------------


How do I get the dimensions of the matrix?
(i.e. what is the equivalent of LBound,UBound for this case)

How do I address the elements of this matrix
(i.e. what is the equivalent of matrix(i,j) for this case)

Thanks a lot,


---
Message posted from http://www.ExcelForum.com/

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Variant as matrix

Hi
Just one extra point; a Variant coming from a single cell value e.g.
MyVariant = Cell.Value cannot be accessed as MyVariant(1, 1). This is
a potential source of pain if you are creating variant arrays inside a
loop for example. If your code can potentially create such a Variant,
you will have to test for it and simply use MyVariant rather than
MyVariant(1,1).
cheers
Paul

curious wrote in message ...
Some relatively easy question:
Suppose I pass a matrix from spreadsheet to VBA as Variant


Code:
--------------------

Public Function foo(matrix As Variant)

End Function

--------------------


How do I get the dimensions of the matrix?
(i.e. what is the equivalent of LBound,UBound for this case)

How do I address the elements of this matrix
(i.e. what is the equivalent of matrix(i,j) for this case)

Thanks a lot,


---
Message posted from http://www.ExcelForum.com/



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 81
Default Variant as matrix

Hi,
could you not use a 2d array?

Ross
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
Variant valiables problem [email protected] Excel Discussion (Misc queries) 0 October 8th 08 10:05 PM
To get a sum for variant currency values [email protected] Excel Discussion (Misc queries) 3 November 24th 07 01:07 PM
Variant Array Steph[_3_] Excel Programming 1 June 2nd 04 01:29 AM
Variant to String Chip Pearson Excel Programming 1 September 3rd 03 03:10 PM
DLLs and VBA: Who free's a variant? Keith Willshaw Excel Programming 0 August 6th 03 09:42 AM


All times are GMT +1. The time now is 07:08 AM.

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"