PC Review


Reply
Thread Tools Rate Thread

Call sub with array

 
 
=?Utf-8?B?QXJuZSBIZWdlZm9ycw==?=
Guest
Posts: n/a
 
      17th Apr 2007
Hi! I have a problem with an array that I have in a udf. I send the array to
another udf in order to sort the array. Somewhere there it goes wrong and the
code stops.
……
Call SortArray(A)
basel = A(2)

End Function
…..
Function SortArray(ByRef TheArray As Variant)
Sorted = False
Do While Not Sorted
Sorted = True
For X = 1 To UBound(TheArray)
If TheArray(X) > TheArray(X + 1) Then
Temp = TheArray(X + 1)
TheArray(X + 1) = TheArray(X)
TheArray(X) = Temp
Sorted = False
End If
Next X
Loop
End Function

The sorting seems to work just fine but the code never goes back to the line
basel = A(2). Instead it goes back to the line before the Call SortArray. I
have no idea what is wrong. Please if anyone can help me! Thanks a lot!

 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      17th Apr 2007
Function SortArray(ByRef TheArray As Variant)
Dim Sorted As Boolean
Dim X As Long
Dim temp
Sorted = False
Do While Not Sorted
Sorted = True
For X = LBound(TheArray) To UBound(TheArray) - 1
If TheArray(X) > TheArray(X + 1) Then
temp = TheArray(X + 1)
TheArray(X + 1) = TheArray(X)
TheArray(X) = temp
Sorted = False
End If
Next X
Loop
End Function



--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Arne Hegefors" <(E-Mail Removed)> wrote in message
news:A67C0AA4-77B0-4BBC-BAB3-(E-Mail Removed)...
> Hi! I have a problem with an array that I have in a udf. I send the array
> to
> another udf in order to sort the array. Somewhere there it goes wrong and
> the
> code stops.
> ..
> Call SortArray(A)
> basel = A(2)
>
> End Function
> ...
> Function SortArray(ByRef TheArray As Variant)
> Sorted = False
> Do While Not Sorted
> Sorted = True
> For X = 1 To UBound(TheArray)
> If TheArray(X) > TheArray(X + 1) Then
> Temp = TheArray(X + 1)
> TheArray(X + 1) = TheArray(X)
> TheArray(X) = Temp
> Sorted = False
> End If
> Next X
> Loop
> End Function
>
> The sorting seems to work just fine but the code never goes back to the
> line
> basel = A(2). Instead it goes back to the line before the Call SortArray.
> I
> have no idea what is wrong. Please if anyone can help me! Thanks a lot!
>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      17th Apr 2007
I'm guessing that you're hiding errors somewhere in your code with an "on error
resume next" line.

This could cause trouble:

For X = 1 To UBound(TheArray)
If TheArray(X) > TheArray(X + 1) Then

Once x is equal to ubound(thearray), then TheArray(X+1) will blow up real good.

This worked ok for me:

Option Explicit
Sub testme()

Dim A As Variant
Dim BaseL As Long

A = Array(11, 5, -2, -3)

On Error Resume Next
Call SortArray(A)
BaseL = A(2)

MsgBox BaseL

End Sub
Function SortArray(ByRef TheArray As Variant)
Dim Sorted As Boolean
Dim X As Long
Dim Temp As Variant

Sorted = False
Do While Not Sorted
Sorted = True
For X = LBound(TheArray) To UBound(TheArray) - 1
If TheArray(X) > TheArray(X + 1) Then
Temp = TheArray(X + 1)
TheArray(X + 1) = TheArray(X)
TheArray(X) = Temp
Sorted = False
End If
Next X
Loop
End Function



Arne Hegefors wrote:
>
> Hi! I have a problem with an array that I have in a udf. I send the array to
> another udf in order to sort the array. Somewhere there it goes wrong and the
> code stops.
> ……
> Call SortArray(A)
> basel = A(2)
>
> End Function
> …..
> Function SortArray(ByRef TheArray As Variant)
> Sorted = False
> Do While Not Sorted
> Sorted = True
> For X = 1 To UBound(TheArray)
> If TheArray(X) > TheArray(X + 1) Then
> Temp = TheArray(X + 1)
> TheArray(X + 1) = TheArray(X)
> TheArray(X) = Temp
> Sorted = False
> End If
> Next X
> Loop
> End Function
>
> The sorting seems to work just fine but the code never goes back to the line
> basel = A(2). Instead it goes back to the line before the Call SortArray. I
> have no idea what is wrong. Please if anyone can help me! Thanks a lot!


--

Dave Peterson
 
Reply With Quote
 
=?Utf-8?B?QXJuZSBIZWdlZm9ycw==?=
Guest
Posts: n/a
 
      17th Apr 2007
Hi Bob! Thanks alot for your answer! I have been spending a whole working day
on this and I still cannot solve it. I used you code but I cannot get it to
work. My code looks like this:

SortArray (A)
basel = Á(2)
End Function
........
Function SortArray(ByRef TheArray As Variant)
Dim Sorted As Boolean
Dim X As Long
Dim B As String
Dim temp
Sorted = False
Do While Not Sorted
Sorted = True
For X = LBound(TheArray) To UBound(TheArray)
If TheArray(X) > TheArray(X + 1) Then
temp = TheArray(X + 1)
TheArray(X + 1) = TheArray(X)
TheArray(X) = temp
Sorted = False
End If
Next X
Loop
End Function

the idea is to sort the array A and then present the number that has
position 2. But there is still someting wrong and I have no idea! If anyone
can help me please do so! Any help appreciated! Thanks alot!

"Bob Phillips" skrev:

> Function SortArray(ByRef TheArray As Variant)
> Dim Sorted As Boolean
> Dim X As Long
> Dim temp
> Sorted = False
> Do While Not Sorted
> Sorted = True
> For X = LBound(TheArray) To UBound(TheArray) - 1
> If TheArray(X) > TheArray(X + 1) Then
> temp = TheArray(X + 1)
> TheArray(X + 1) = TheArray(X)
> TheArray(X) = temp
> Sorted = False
> End If
> Next X
> Loop
> End Function
>
>
>
> --
> ---
> HTH
>
> Bob
>
> (there's no email, no snail mail, but somewhere should be gmail in my addy)
>
>
>
> "Arne Hegefors" <(E-Mail Removed)> wrote in message
> news:A67C0AA4-77B0-4BBC-BAB3-(E-Mail Removed)...
> > Hi! I have a problem with an array that I have in a udf. I send the array
> > to
> > another udf in order to sort the array. Somewhere there it goes wrong and
> > the
> > code stops.
> > ..
> > Call SortArray(A)
> > basel = A(2)
> >
> > End Function
> > ...
> > Function SortArray(ByRef TheArray As Variant)
> > Sorted = False
> > Do While Not Sorted
> > Sorted = True
> > For X = 1 To UBound(TheArray)
> > If TheArray(X) > TheArray(X + 1) Then
> > Temp = TheArray(X + 1)
> > TheArray(X + 1) = TheArray(X)
> > TheArray(X) = Temp
> > Sorted = False
> > End If
> > Next X
> > Loop
> > End Function
> >
> > The sorting seems to work just fine but the code never goes back to the
> > line
> > basel = A(2). Instead it goes back to the line before the Call SortArray.
> > I
> > have no idea what is wrong. Please if anyone can help me! Thanks a lot!
> >

>
>
>

 
Reply With Quote
 
=?Utf-8?B?VmVyZ2VsIEFkcmlhbm8=?=
Guest
Posts: n/a
 
      17th Apr 2007
Hi Arne,

Give this a try.

Change your function declaration to become like this:

Function SortArray(ByVal TheArray As Variant) as Variant

Then, before the "End Function" line, put this line:

SortArray = TheArray

To use your new function, you do something like this

A = SortArray(A)
basel = A(2)


--
Hope that helps.

Vergel Adriano


"Arne Hegefors" wrote:

> Hi! I have a problem with an array that I have in a udf. I send the array to
> another udf in order to sort the array. Somewhere there it goes wrong and the
> code stops.
> ……
> Call SortArray(A)
> basel = A(2)
>
> End Function
> …..
> Function SortArray(ByRef TheArray As Variant)
> Sorted = False
> Do While Not Sorted
> Sorted = True
> For X = 1 To UBound(TheArray)
> If TheArray(X) > TheArray(X + 1) Then
> Temp = TheArray(X + 1)
> TheArray(X + 1) = TheArray(X)
> TheArray(X) = Temp
> Sorted = False
> End If
> Next X
> Loop
> End Function
>
> The sorting seems to work just fine but the code never goes back to the line
> basel = A(2). Instead it goes back to the line before the Call SortArray. I
> have no idea what is wrong. Please if anyone can help me! Thanks a lot!
>

 
Reply With Quote
 
=?Utf-8?B?VmVyZ2VsIEFkcmlhbm8=?=
Guest
Posts: n/a
 
      17th Apr 2007
Never mind. I misread your post. Bob's code worked.

In case you missed what he changed, change this line in your code:

For X = LBound(TheArray) To UBound(TheArray)

to like this

For X = LBound(TheArray) To UBound(TheArray)-1



--
Hope that helps.

Vergel Adriano


"Vergel Adriano" wrote:

> Hi Arne,
>
> Give this a try.
>
> Change your function declaration to become like this:
>
> Function SortArray(ByVal TheArray As Variant) as Variant
>
> Then, before the "End Function" line, put this line:
>
> SortArray = TheArray
>
> To use your new function, you do something like this
>
> A = SortArray(A)
> basel = A(2)
>
>
> --
> Hope that helps.
>
> Vergel Adriano
>
>
> "Arne Hegefors" wrote:
>
> > Hi! I have a problem with an array that I have in a udf. I send the array to
> > another udf in order to sort the array. Somewhere there it goes wrong and the
> > code stops.
> > ……
> > Call SortArray(A)
> > basel = A(2)
> >
> > End Function
> > …..
> > Function SortArray(ByRef TheArray As Variant)
> > Sorted = False
> > Do While Not Sorted
> > Sorted = True
> > For X = 1 To UBound(TheArray)
> > If TheArray(X) > TheArray(X + 1) Then
> > Temp = TheArray(X + 1)
> > TheArray(X + 1) = TheArray(X)
> > TheArray(X) = Temp
> > Sorted = False
> > End If
> > Next X
> > Loop
> > End Function
> >
> > The sorting seems to work just fine but the code never goes back to the line
> > basel = A(2). Instead it goes back to the line before the Call SortArray. I
> > have no idea what is wrong. Please if anyone can help me! Thanks a lot!
> >

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      17th Apr 2007
A littel bit more than that, it was

For X = 1 To UBound(TheArray)

to

For X = LBound(TheArray) To UBound(TheArray)-1



--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Vergel Adriano" <(E-Mail Removed)> wrote in message
news:0C40F8E6-5C1C-47EC-B951-(E-Mail Removed)...
> Never mind. I misread your post. Bob's code worked.
>
> In case you missed what he changed, change this line in your code:
>
> For X = LBound(TheArray) To UBound(TheArray)
>
> to like this
>
> For X = LBound(TheArray) To UBound(TheArray)-1
>
>
>
> --
> Hope that helps.
>
> Vergel Adriano
>
>
> "Vergel Adriano" wrote:
>
>> Hi Arne,
>>
>> Give this a try.
>>
>> Change your function declaration to become like this:
>>
>> Function SortArray(ByVal TheArray As Variant) as Variant
>>
>> Then, before the "End Function" line, put this line:
>>
>> SortArray = TheArray
>>
>> To use your new function, you do something like this
>>
>> A = SortArray(A)
>> basel = A(2)
>>
>>
>> --
>> Hope that helps.
>>
>> Vergel Adriano
>>
>>
>> "Arne Hegefors" wrote:
>>
>> > Hi! I have a problem with an array that I have in a udf. I send the
>> > array to
>> > another udf in order to sort the array. Somewhere there it goes wrong
>> > and the
>> > code stops.
>> > ..
>> > Call SortArray(A)
>> > basel = A(2)
>> >
>> > End Function
>> > ...
>> > Function SortArray(ByRef TheArray As Variant)
>> > Sorted = False
>> > Do While Not Sorted
>> > Sorted = True
>> > For X = 1 To UBound(TheArray)
>> > If TheArray(X) > TheArray(X + 1) Then
>> > Temp = TheArray(X + 1)
>> > TheArray(X + 1) = TheArray(X)
>> > TheArray(X) = Temp
>> > Sorted = False
>> > End If
>> > Next X
>> > Loop
>> > End Function
>> >
>> > The sorting seems to work just fine but the code never goes back to the
>> > line
>> > basel = A(2). Instead it goes back to the line before the Call
>> > SortArray. I
>> > have no idea what is wrong. Please if anyone can help me! Thanks a lot!
>> >



 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      17th Apr 2007
Aaah, don't put the array in brackets, use

SortArray A

or

Call SortArray(A)

but not

SortArray(A)

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Arne Hegefors" <(E-Mail Removed)> wrote in message
news:4CDFC425-4358-44B5-8537-(E-Mail Removed)...
> Hi Bob! Thanks alot for your answer! I have been spending a whole working
> day
> on this and I still cannot solve it. I used you code but I cannot get it
> to
> work. My code looks like this:
>
> SortArray (A)
> basel = (2)
> End Function
> .......
> Function SortArray(ByRef TheArray As Variant)
> Dim Sorted As Boolean
> Dim X As Long
> Dim B As String
> Dim temp
> Sorted = False
> Do While Not Sorted
> Sorted = True
> For X = LBound(TheArray) To UBound(TheArray)
> If TheArray(X) > TheArray(X + 1) Then
> temp = TheArray(X + 1)
> TheArray(X + 1) = TheArray(X)
> TheArray(X) = temp
> Sorted = False
> End If
> Next X
> Loop
> End Function
>
> the idea is to sort the array A and then present the number that has
> position 2. But there is still someting wrong and I have no idea! If
> anyone
> can help me please do so! Any help appreciated! Thanks alot!
>
> "Bob Phillips" skrev:
>
>> Function SortArray(ByRef TheArray As Variant)
>> Dim Sorted As Boolean
>> Dim X As Long
>> Dim temp
>> Sorted = False
>> Do While Not Sorted
>> Sorted = True
>> For X = LBound(TheArray) To UBound(TheArray) - 1
>> If TheArray(X) > TheArray(X + 1) Then
>> temp = TheArray(X + 1)
>> TheArray(X + 1) = TheArray(X)
>> TheArray(X) = temp
>> Sorted = False
>> End If
>> Next X
>> Loop
>> End Function
>>
>>
>>
>> --
>> ---
>> HTH
>>
>> Bob
>>
>> (there's no email, no snail mail, but somewhere should be gmail in my
>> addy)
>>
>>
>>
>> "Arne Hegefors" <(E-Mail Removed)> wrote in message
>> news:A67C0AA4-77B0-4BBC-BAB3-(E-Mail Removed)...
>> > Hi! I have a problem with an array that I have in a udf. I send the
>> > array
>> > to
>> > another udf in order to sort the array. Somewhere there it goes wrong
>> > and
>> > the
>> > code stops.
>> > ..
>> > Call SortArray(A)
>> > basel = A(2)
>> >
>> > End Function
>> > ...
>> > Function SortArray(ByRef TheArray As Variant)
>> > Sorted = False
>> > Do While Not Sorted
>> > Sorted = True
>> > For X = 1 To UBound(TheArray)
>> > If TheArray(X) > TheArray(X + 1) Then
>> > Temp = TheArray(X + 1)
>> > TheArray(X + 1) = TheArray(X)
>> > TheArray(X) = Temp
>> > Sorted = False
>> > End If
>> > Next X
>> > Loop
>> > End Function
>> >
>> > The sorting seems to work just fine but the code never goes back to the
>> > line
>> > basel = A(2). Instead it goes back to the line before the Call
>> > SortArray.
>> > I
>> > have no idea what is wrong. Please if anyone can help me! Thanks a lot!
>> >

>>
>>
>>



 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      17th Apr 2007
and this

For X = LBound(TheArray) To UBound(TheArray)

should be

For X = LBound(TheArray) To UBound(TheArray) - 1

as I gave you

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Arne Hegefors" <(E-Mail Removed)> wrote in message
news:4CDFC425-4358-44B5-8537-(E-Mail Removed)...
> Hi Bob! Thanks alot for your answer! I have been spending a whole working
> day
> on this and I still cannot solve it. I used you code but I cannot get it
> to
> work. My code looks like this:
>
> SortArray (A)
> basel = (2)
> End Function
> .......
> Function SortArray(ByRef TheArray As Variant)
> Dim Sorted As Boolean
> Dim X As Long
> Dim B As String
> Dim temp
> Sorted = False
> Do While Not Sorted
> Sorted = True
> For X = LBound(TheArray) To UBound(TheArray)
> If TheArray(X) > TheArray(X + 1) Then
> temp = TheArray(X + 1)
> TheArray(X + 1) = TheArray(X)
> TheArray(X) = temp
> Sorted = False
> End If
> Next X
> Loop
> End Function
>
> the idea is to sort the array A and then present the number that has
> position 2. But there is still someting wrong and I have no idea! If
> anyone
> can help me please do so! Any help appreciated! Thanks alot!
>
> "Bob Phillips" skrev:
>
>> Function SortArray(ByRef TheArray As Variant)
>> Dim Sorted As Boolean
>> Dim X As Long
>> Dim temp
>> Sorted = False
>> Do While Not Sorted
>> Sorted = True
>> For X = LBound(TheArray) To UBound(TheArray) - 1
>> If TheArray(X) > TheArray(X + 1) Then
>> temp = TheArray(X + 1)
>> TheArray(X + 1) = TheArray(X)
>> TheArray(X) = temp
>> Sorted = False
>> End If
>> Next X
>> Loop
>> End Function
>>
>>
>>
>> --
>> ---
>> HTH
>>
>> Bob
>>
>> (there's no email, no snail mail, but somewhere should be gmail in my
>> addy)
>>
>>
>>
>> "Arne Hegefors" <(E-Mail Removed)> wrote in message
>> news:A67C0AA4-77B0-4BBC-BAB3-(E-Mail Removed)...
>> > Hi! I have a problem with an array that I have in a udf. I send the
>> > array
>> > to
>> > another udf in order to sort the array. Somewhere there it goes wrong
>> > and
>> > the
>> > code stops.
>> > ..
>> > Call SortArray(A)
>> > basel = A(2)
>> >
>> > End Function
>> > ...
>> > Function SortArray(ByRef TheArray As Variant)
>> > Sorted = False
>> > Do While Not Sorted
>> > Sorted = True
>> > For X = 1 To UBound(TheArray)
>> > If TheArray(X) > TheArray(X + 1) Then
>> > Temp = TheArray(X + 1)
>> > TheArray(X + 1) = TheArray(X)
>> > TheArray(X) = Temp
>> > Sorted = False
>> > End If
>> > Next X
>> > Loop
>> > End Function
>> >
>> > The sorting seems to work just fine but the code never goes back to the
>> > line
>> > basel = A(2). Instead it goes back to the line before the Call
>> > SortArray.
>> > I
>> > have no idea what is wrong. Please if anyone can help me! Thanks a lot!
>> >

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?QXJuZSBIZWdlZm9ycw==?=
Guest
Posts: n/a
 
      18th Apr 2007
Hi! Thank you very much for all your help! I would never I have solved it
myself! Your code works fine but I have final question. If all the cells that
I am taking in as argument in the main function are empty or non valid then I
get an error message in the Excel sheet. However I would like to display
"n/a". The variable k keeps track of the number of valid cells but I have not
managed to solve it but simply writing

if k = 0 then
basel = "n/a"
end if

If anyone could help me with this I would be most grateful. Again thanks for
all your help!!!

"Bob Phillips" skrev:

> A littel bit more than that, it was
>
> For X = 1 To UBound(TheArray)
>
> to
>
> For X = LBound(TheArray) To UBound(TheArray)-1
>
>
>
> --
> ---
> HTH
>
> Bob
>
> (there's no email, no snail mail, but somewhere should be gmail in my addy)
>
>
>
> "Vergel Adriano" <(E-Mail Removed)> wrote in message
> news:0C40F8E6-5C1C-47EC-B951-(E-Mail Removed)...
> > Never mind. I misread your post. Bob's code worked.
> >
> > In case you missed what he changed, change this line in your code:
> >
> > For X = LBound(TheArray) To UBound(TheArray)
> >
> > to like this
> >
> > For X = LBound(TheArray) To UBound(TheArray)-1
> >
> >
> >
> > --
> > Hope that helps.
> >
> > Vergel Adriano
> >
> >
> > "Vergel Adriano" wrote:
> >
> >> Hi Arne,
> >>
> >> Give this a try.
> >>
> >> Change your function declaration to become like this:
> >>
> >> Function SortArray(ByVal TheArray As Variant) as Variant
> >>
> >> Then, before the "End Function" line, put this line:
> >>
> >> SortArray = TheArray
> >>
> >> To use your new function, you do something like this
> >>
> >> A = SortArray(A)
> >> basel = A(2)
> >>
> >>
> >> --
> >> Hope that helps.
> >>
> >> Vergel Adriano
> >>
> >>
> >> "Arne Hegefors" wrote:
> >>
> >> > Hi! I have a problem with an array that I have in a udf. I send the
> >> > array to
> >> > another udf in order to sort the array. Somewhere there it goes wrong
> >> > and the
> >> > code stops.
> >> > ..
> >> > Call SortArray(A)
> >> > basel = A(2)
> >> >
> >> > End Function
> >> > ...
> >> > Function SortArray(ByRef TheArray As Variant)
> >> > Sorted = False
> >> > Do While Not Sorted
> >> > Sorted = True
> >> > For X = 1 To UBound(TheArray)
> >> > If TheArray(X) > TheArray(X + 1) Then
> >> > Temp = TheArray(X + 1)
> >> > TheArray(X + 1) = TheArray(X)
> >> > TheArray(X) = Temp
> >> > Sorted = False
> >> > End If
> >> > Next X
> >> > Loop
> >> > End Function
> >> >
> >> > The sorting seems to work just fine but the code never goes back to the
> >> > line
> >> > basel = A(2). Instead it goes back to the line before the Call
> >> > SortArray. I
> >> > have no idea what is wrong. Please if anyone can help me! Thanks a lot!
> >> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: API function call with array pointer as argument zhilko@ufacom.ru Microsoft C# .NET 3 11th Jun 2007 06:56 PM
How do I call Excel functions with an array parameter? =?Utf-8?B?TGFUYW55YQ==?= Microsoft Access VBA Modules 3 3rd Mar 2005 11:04 PM
C# to COM interop call returning an array of objects. Interop newbie Microsoft C# .NET 2 27th Nov 2003 05:37 PM
param array and stored procedure call M Microsoft C# .NET 7 16th Sep 2003 05:38 AM
param array and stored procedure call M Microsoft ADO .NET 7 16th Sep 2003 05:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:09 PM.