Posted to microsoft.public.excel.programming
|
|
Re-entrant function
May I suggest you write it as 1-Dim, and only transpose if needed.
This allows for only 1 major code to write.
The function "Transpose" is so common, I have it as a Library funtion Tr1, &
Tr2.
Again, just some ideas.
Sub Example()
Dim Va
Dim Vb
Dim CP(1 To 3)
Va = Array(1, 2, 3)
Vb = Array(4, 5, 7)
'// Lower Bound is 0.
'// Let's make it 1 for ease of use
Va = Tr2(Va)
Vb = Tr2( Vb)
CP(1) = Va(2) * Vb(3) - Va(3) * Vb(2)
CP(2) = Va(3) * Vb(1) - Va(1) * Vb(3)
CP(3) = Va(1) * Vb(2) - Va(2) * Vb(1)
If Selection.Columns.Count 1 Then
ActiveCell.Resize(1, 3) = CP 'Horizontal
Else
ActiveCell.Resize(3, 1) = Tr1(CP) 'Verticle
End If
End Sub
Function Tr1(v)
' Transpose 1 time.
With WorksheetFunction
Tr1 = .Transpose(v)
End With
End Function
Function Tr2(v)
' Transpose 2 times.
With WorksheetFunction
Tr2 = .Transpose(.Transpose(v))
End With
End Function
--
HTH :)
Dana DeLouis
"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
|