XL: Visual Basic Function to Calculate Cross Product

Last reviewed: February 3, 1998
Article ID: Q121820
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

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: 5.00 7.00 XL98 XL97 XL7 XL5
Keywords : kbcode kbprg xlvbahowto xlformula PgmHowto PgmOthr
Version : WINDOWS:5.0,5.0a,5.0c,7.0,97; MACINTOSH:5.0,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.