excelvectorcross-product# VBA for Cross Products in Excel

It seems that for some reason Excel programmers chose to omit any vector cross-product functionality.

Furthermore, looking online, there isn't really much demand for it. Although excel is a powerful tool to do linear algebra.

I need a VBA script to do Vector Cross Products. And the only one I could find was from here:

EDIT:

- To add a VBA script, press Alt + F11
- Under Project, right click on VBAProject and Insert->Module
- Save, click No to save as a Macro-Enabled Workbook
- Save as an .xlsm file

```
--------------------
Function vCP(v1 As Variant, v2 As Variant) As Variant
vCP = Array(v1(2) * v2(3) - v1(3) * v2(2), _
v1(3) * v2(1) - v1(1) * v2(3), _
v1(1) * v2(2) - v1(2) * v2(1))
End Function
--------------------
```

Using it is simple,

- select 3 horizontal adjacent cells, type in formula
`=vCP(`

- Select vector A (in A x B) which is in either 3 consecutive horizontal or vertical cells
- type
`,`

- Select vector B, which is either 3 consecutive horizontal of vertical cells
- type
`)`

- Press Ctrl+Shift+Enter

I did a couple tests on it, and it works, but it outputs a horizontal vector, not vertical, the way it's preferred for Linear Algebra.

Does anyone know how to change this script so the 3D vectors can be output vertically instead?

Is there a better way to get a cross product in Excel?

Thanks, -D

Solution

Use Application.Transpose:

```
Function vCP(v1 As Variant, v2 As Variant) As Variant
vCP = Application.Transpose(Array(v1(2) * v2(3) - v1(3) * v2(2), _
v1(3) * v2(1) - v1(1) * v2(3), _
v1(1) * v2(2) - v1(2) * v2(1)))
End Function
```

- Registering COM Object on Windows 64-bit For Use In Excel-32 Bit Is Failing
- Shift data in excel Sheet to left to remove empty cells and include Header names
- If statement in Worksheet_Change not working with On Error Resume Next
- is there a excel formula which will give me the highest year from column F, corresponding to every company code in column c
- Parse Full Name Into Parts
- Transfer data to other sheets/ranges based on dropdown menu on home page
- Excel formula to check a text value from a cell against table's first column and return a value from the table second column
- Excel Multiple Ranges - need different answers for each range (step function)
- VBA Workbooks stop working with upgrade to Excel 365 - VBA Compiling Issue?
- How can I search a range of multiple rows and columns for a value and return it's header?
- If File = "False" Application.GetOpenFileName Error 13 Type Mismatch
- Calculating percent error of each element of a list given a list of ratios
- SWITCH Function with multiple hits - Power Query
- Recursive Lambda to join full parent-child hierarchy
- Excel Conditional Formatting with PowerShell
- How to sort an Excel array in place
- No Such Interface supported while copying worksheets from source Workbook to destination Workbook - Excel VSTO Addin
- Excel formula for complex 2d table sum
- Export Word document as PDF using Excel VBA
- Lookup multiple criteria from column 2 in a table and return the value in column 1 if all match
- Why is the page header not saved when I save and close a workbook?
- ADODB recordset recordcount always returns -1
- Sum cell values below until a blank cell with Python
- VBA empty rows getting filled after running code
- Laravel Excel upload and progressbar
- FileSystem.FileCopy vs objFSO.CopyFile
- Excel VBA: Private Sub procedure too large
- VBA Checkbox combination enables not right?
- Excel macro works on one file, but not for multiple files when looping through a folder
- ASP.NET MVC FileResult is corrupting files