View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
Dana DeLouis Dana DeLouis is offline
external usenet poster
 
Posts: 947
Default Re-entrant function

... but it crashes if the selecion is verticle.

I selected the verticle range B5:B7 also, and Array entered this with no
problem ??

=Cross(A1:A3,C1:E1)

--
Dana DeLouis


"LesHurley" wrote in message
...
Bob & Dana, thanks for your ongoing effort. My object is to be able to
pass
any two two 3-d vectors as arguments to the function CrossProd. "Any",
means
horizontal or verticle or one, either one, of each so long as they are
contiguous cells in EXCEL. Executing =CrossProd(j,k) is never a problem,
Executing =CrossProd(CrossProd(J,K),CrossProd(l,m)) works ok too provided
the
cells selected for the answer are horizontal, but it crashes if the
selecion
is verticle. I think it has something to do with the vectors received by
the
function on the third pass are variant arrays rather than Range objects.
I
have tried to convert the variant arrays to Range with no success. To
answer
Bob's question specifically, just pick out any three numbers in EXCEL to
fill
the 3-d vectors and the function must work to fulfill my objective,
Thanks
again.
--
Thanks for your help


"Dana DeLouis" wrote:

My guess is that the input ranges are either verticle, horizontal, or
mixed.
In this test, I selected 3 horizontal cells, and "Array Entered" this
equation:

=Cross(A1:A3,C1:E1) (Ctrl+Shift+Enter)

Function Cross(xx, yy)
'// = = = = = = =
'// Cross Product
'// = = = = = = =

Dim x As Double
Dim y As Double
Dim CP(1 To 3)

x = xx.Value
y = yy.Value

'// Put input into 1-Dim form
If xx.Rows.Count 1 Then
x = Tr1(x)
Else
x = Tr2(x)
End If

If yy.Rows.Count 1 Then
y = Tr1(y)
Else
y = Tr2(y)
End If

CP(1) = x(2) * y(3) - x(3) * y(2)
CP(2) = x(3) * y(1) - x(1) * y(3)
CP(3) = x(1) * y(2) - x(2) * y(1)

If Application.Caller.Columns.Count 1 Then
Cross = CP 'Horizontal
Else
Cross = Tr1(CP) 'Verticle
End If
End Function

--
HTH :)
Dana DeLouis


"LesHurley" wrote in message
...
Bob, Dana is using a sub and supplying the data in the procedure.
Dana's
Tr2
works OK with VBA arrays but not with Range Objects passed from EXCEL
as
arguments. Any pair of 3-d vectors are fine for Va and Vb but they
must
come
from EXCEL. That's what the procedure is for. Please see my opening
post
for details. Only one change there that shouldn't matter anyway:
change
the
Static to Dim on r and c.
Thanks for your help


"Bob Phillips" wrote:

Les,

I tested it and it seemed to work okay.

What is the data that is erroring?

--
---
HTH

Bob


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



"LesHurley" wrote in message
...
Sorry Bob, that doesn't work any different than before. Still
crashes
when
it tries to make the assignment at line 9 (between the two
debug.Print
...
ststements).
--
Thanks for your help


"Bob Phillips" wrote:

Function XX(Va, Vb)
Dim CP() As Double
Static r As Integer, c As Integer

Debug.Print TypeName(Va), TypeName(Vb)

r = Selection.Rows.Count
c = Selection.Columns.Count

If r c Then
ReDim CP(3, 1)
Debug.Print "Got to line 8 OK"
CP(1, 1) = Va(2) * Vb(3) - Va(3) * Vb(2)
Debug.Print "Got to line 10 OK"
CP(2, 1) = Va(3) * Vb(1) - Va(1) * Vb(3)
CP(3, 1) = Va(1) * Vb(2) - Va(2) * Vb(1)
Debug.Print r, CP(1, 1), CP(2, 1), CP(3, 1)
XX = Application.Transpose(Application.Transpose(CP))
Else
ReDim CP(1, 3)
CP(1, 1) = Va(2) * Vb(3) - Va(3) * Vb(2)
CP(1, 2) = Va(3) * Vb(1) - Va(1) * Vb(3)
CP(1, 3) = Va(1) * Vb(2) - Va(2) * Vb(1)
Debug.Print r, CP(1, 1), CP(1, 2), CP(1, 3)
XX = CP
End If
End Function


--
---
HTH

Bob


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



"LesHurley" wrote in message
...
Several weeks ago I posted a version of this and I thought I had
it
solved
with help from this DG - but not so!

xx(j,k) calculates the crossproduct of two 3-dimensional vectors.
the
result is returned in either a row or column on the work sheet as
selected
by
the user. Normally the function works ok. It even works ok in
"=xx(xx(i,j),xx(k,l)) when the selection is a row but crashes at
line
9,
on
the third pass (Va and Vb are then Variants) when the selection
is
a
column.
I'm using Option Base 1.

Function XX(Va, Vb)
Debug.Print TypeName(Va), TypeName(Vb)
Static r As Integer, c As Integer

r = Selection.Rows.Count
c = Selection.Columns.Count

Dim CP() As Double
If r c Then
ReDim CP(3, 1)
8 MsgBox "Got to line 8 OK"
9 CP(1, 1) = Va(2) * Vb(3) - Va(3) * Vb(2)
10 MsgBox "Got to line 10 OK"
CP(2, 1) = Va(3) * Vb(1) - Va(1) * Vb(3)
CP(3, 1) = Va(1) * Vb(2) - Va(2) * Vb(1)
Debug.Print r, CP(1, 1), CP(2, 1), CP(3, 1)
Else
ReDim CP(1, 3)
11 CP(1, 1) = Va(2) * Vb(3) - Va(3) * Vb(2)
CP(1, 2) = Va(3) * Vb(1) - Va(1) * Vb(3)
CP(1, 3) = Va(1) * Vb(2) - Va(2) * Vb(1)
Debug.Print r, CP(1, 1), CP(1, 2), CP(1, 3)
End If
XX = CP
End Function

Can anyone help?

--
Thanks for your help