Skip to content

Instantly share code, notes, and snippets.

@AbeKh
Last active October 17, 2023 18:20
Show Gist options
  • Save AbeKh/669b2371ad92593976c5db3f63057cef to your computer and use it in GitHub Desktop.
Save AbeKh/669b2371ad92593976c5db3f63057cef to your computer and use it in GitHub Desktop.
custom Excel function (also known as a User-Defined Function or UDF) to perform error propagation for multiplication or division.
'1. Press `ALT` + `F11` to open the Visual Basic for Applications (VBA) editor in Excel.
'2. In the VBA editor, click `Insert` > `Module` to insert a new module.
'3. Copy and paste the following VBA code into the module:
Function PropagateError(operation As String, value1 As Double, sigma1 As Double, value2 As Double, sigma2 As Double) As Double
Dim result As Double
Select Case operation
Case "multiply"
result = value1 * value2
PropagateError = result * Sqr((sigma1 / value1) ^ 2 + (sigma2 / value2) ^ 2)
Case "divide"
result = value1 / value2
PropagateError = result * Sqr((sigma1 / value1) ^ 2 + (sigma2 / value2) ^ 2)
Case Else
PropagateError = CVErr(xlErrValue) ' Return an error if the operation is not recognized
End Select
End Function
'4. Close the VBA editor and return to your Excel worksheet.
'You can use this custom function in your worksheet to calculate the propagated standard deviation for multiplication or division.
'For example, if you want to multiply two values `A1` and `A2` with standard deviations `B1` and `B2`, you can use the function as follows:
- 'To calculate the result of the multiplication:
=PropagateError("multiply", A1, B1, A2, B2)
- 'To calculate the result of the division:
=PropagateError("divide", A1, B1, A2, B2)
`Replace`A1`,`A2`,`B1`, and`B2`with the appropriate cell references in your Excel worksheet. The function`will return the propagated standard deviation for the specified operation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment