Microsoft KB Archive/121820

= Visual Basic Function to Calculate Cross Product =

Article ID: 121820

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0 Standard Edition
 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q121820




 * Microsoft Excel for Windows 5.0c|5.0c
 * Microsoft Excel for Windows NT 5.0|5.0
 * Microsoft Excel for Windows 95 7.0a|7.0a
 * Microsoft Excel for the Macintosh 5.0|5.0
 * Microsoft Excel for the Macintosh 5.0a|5.0a



SUMMARY
This article contains a sample Microsoft Visual Basic for Applications function that takes two arrays (three rows by one column) of numbers, each representing a vector, and returns an array of the same dimensions representing the cross product of the two vectors.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The cross product, c = a x b, of the vectors a and b is a vector that is perpendicular to the plane of a and b. It can be illustrated by the following table:            i   j   k            ========= c = a x b = m  n   o  = [(n*z)-(o*y)]i - [(o*x)-(m*z)]j + [(m*y)-(n*x)]k x  y   z For example, given two vectors a and b:    a = (1,2,3) b = (4,5,6) vector c can be computed:   c = [(2*6)-(3*5)]i - [(3*4)-(1*6)]j + [(1*5)-(2*4)]k = [12-15]i - [12-6]j + [5-8]k = [-3]i - [6]j + [-3]k = (-3,6,-3) In Microsoft Excel, you can create a Visual Basic function to perform these calculations and return the results into an array.

Sample Visual Basic Procedure
' The following line must be placed at beginning of module: Option Base 1

' Returns the cross product of the two input vectors. Function Cross_Product_VBA(Vec1 As Object, Vec2 As Object) As Variant ' TempArray is a temporary variable to store the cross product. Dim TempArray(3, 1)

' The following lines put elements of the product into TempArray. TempArray(1, 1) = Vec1.Cells(2, 1).Value * _ Vec2.Cells(3, 1).Value - Vec1.Cells(3, 1).Value * _ Vec2.Cells(2, 1).Value TempArray(2, 1) = Vec1.Cells(3, 1).Value * _ Vec2.Cells(1, 1).Value - Vec1.Cells(1, 1).Value * _ Vec2.Cells(3, 1).Value TempArray(3, 1) = Vec1.Cells(1, 1).Value * _ Vec2.Cells(2, 1).Value - Vec1.Cells(2, 1).Value * _ Vec2.Cells(1, 1).Value

' Assigns the TempArray variable to Cross_Product_VBA. Cross_Product_VBA = TempArray

End Function Be sure to select three vertical cells before typing the function, and enter the function as an array formula by pressing CTRL+SHIFT+ENTER or COMMAND+RETURN. For example, given the ranges A1:A3 (vector a) and B1:B3 (vector b), calculate the cross product by highlighting C1:C3, type the following function, and press CTRL+SHIFT+ENTER or COMMAND+RETURN:

=Cross_Product_VBA(A1:A3,B1:B3)

The results are shown below:   A1: 1     B1: 4     C1: -3 A2: 2    B2: 5     C2:  6 A3: 3    B3: 6     C3: -3

Additional query words: XL98 XL97 XL7 XL5 XL

Keywords: kbdtacode kbhowto kbprogramming KB121820

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.