Re: Array Sorting but return the index number as well

Tech-Archive recommends: Speed Up your PC by fixing your registry




<atmkoh@xxxxxxxxxxx> wrote in message
news:1161996939.530324.171760@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks Larry worked a treat!
I will study the code and try to learn as much as I can.

On a related matter, how do you actually pass an array to a function?
Why do i get the error message?
Private Sub Command1_Click()

ReDim A(10, 5)
ReDim B(10, 5)
Dim i
Dim j

For i = 1 To 10
For j = 1 To 5
A(i, j) = Rnd
Next j
Next i

'What I am having trouble is:

B = square(A)
End Sub

Public Function square(A As Variant) As Variant
square = A ^ 2
End Function

'How do I write the function square such that for every element of A
that is passed in is squared and the number of elements that is passed
in is fixed.? What I am doing at present is passing it in as element
one by one!! Obviously there must be a method to square it as a matrix
right? I always get a type mismatch error. I dont understand why?

I was thinking of using a UBOUND function

Public Function square(A As Variant) As Variant
for i = 1 to UBound(A) ......

square(i,j) = A(i,j) ^ 2
End Function

But what do I give for the dimensions of j or can anyone advise a
better method?

Grateful for any help!


It's not clear for me what you are trying to accomplish. If you want to
return an array of the same size with elements equal to squares of
respective elements of the source then it is something like this (air code -
means typed right here without testing):

private sub Test()
'if you work with integer values - replace it with Long of whatever will
fit your needs
dim arSrc() as Double'dynamic array
dim arSquare() as Double
redim A(10,5)
arSquare=GetSquare(arSrc)
end sub

private function GetSquare(arSrc() as Double) as Double()
dim arRes() as Double
dim i as long, j as long
dim iMin as long, iMax as long
dim jMin as long, jMax as long
iMin=lbound(arSrc,1) : iMax=ubound(arSrc,1)
jMin=lbound(arSrc,2) : jMax=ubound(arSrc,2)
redim arRes(iMin to iMax, jMin to jMax)
for i=iMin to iMax
for j=jMin to jMax
arRes(i,j)=arSrc(i,j) * arSrc(i,j)
next j
next i
GetSquare=arRes
end function

Some [kind of OT] notes. I intentionally don't use Variants. You can still
use it - just replace Double() with Variant. But it is highly recommended to
use primitive data types, especially for extensive math operations
involving loops. Using Variant everywhere is very convenient way to degrade
you application's performance and memory usage, unless you know exactly when
and how to use them efficiently.
In your code it seems, that you use either global or module level variables,
where local variables would suffice, or you just don't use Option Explicit
and rely on autoinstancing of variables. Both are extremely dangerous
techniques, which you should drop without even starting.
Place Option Explicit statement at the beginning of each of your modules and
use explicit variables declarations (you can "ask" VB to do it automatically
for all new modules - Tools->Options-> check "Require Variable
Declaration"). Redim has unfortunate property to work as an implicit dim,
but there is nothing good about it - you still should use dim explicitly.

Dmitriy.




.



Relevant Pages

  • Re: Array Declaration Problem ??
    ... the declaration ReDim awas commented out in Function Zroots. ... Function ZrootsAs Variant() ... Dim j As Integer, its As Integer ... Enter on a worksheet the function "MyRoots() and select a 4 Row*2 Column ...
    (microsoft.public.excel.programming)
  • Re: Array Declaration Problem ??
    ... Function MyRoots (a As Variant, m As Integer, polish As String) ... Dim Roots As Variant ... ReDim ad ...
    (microsoft.public.excel.programming)
  • Re: Re:workdays
    ... Optional Holidays As Variant = Nothing, ... Dim arrayH As Variant ... ' or not an array or cell range with at least one numeric value between ... ReDim arrayH) As Variant ...
    (microsoft.public.excel)
  • Re: How do I Create ragged arrays in Excel VBA?
    ... Sub RaggedArray() ... Dim AAs Variant, i As Long, j As Long ... As Variant ... ReDim A ...
    (microsoft.public.excel.programming)
  • Re: shape range help
    ... However initial use of Dim() avoided use of brackets and he ... Everytime with ReDim only, I get a Variant() ... never in a session I don't do the initial Dim varr() then redim does NOT ...
    (microsoft.public.excel.programming)