Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I think I can use that Dana. Thanks.
-- Thanks for your help "Dana DeLouis" wrote: 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 |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Dana; I tried out your suggestion. It didn't work. Then I simplified it as
follows: Function XX(Va, Vb) Va = Tr2(Va) Vb = Tr2(Vb) For i = 1 To 3 10 Debug.Print "Inside for/next loop" 11 Debug.Print Va(i), Vb(i) ' It crashes here on the first time thru the loop. Next i End Function Back to the drawing board. Les -- Thanks for your help "Dana DeLouis" wrote: 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 |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
As I said in my tests it worked fine, Dana's offering has nothing to do with
my questions. I was hoping that you could supply some sample data that I could test with so that I could see why it doesn't work for you. -- --- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "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 |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Excel Data Validation/Lookup function does function correcty | Excel Worksheet Functions | |||
Re-entrant call to VBA | Excel Programming | |||
User Function Question: Collect Condition in Dialog Box - But How toInsert into Function Equation? | Excel Programming | |||
LINKEDRANGE function - a complement to the PULL function (for getting values from a closed workbook) | Excel Worksheet Functions | |||
Excel - User Defined Function Error: This function takes no argume | Excel Programming |