Code Generator | VBA

VBA Pressure Unit Converter

This document details a VBA function for converting various pressure units (Pascals, Atmospheres, Bar, Torr, PSI) with input validation and error handling, ensuring robust and efficient unit conversion in Excel.


Empty image or helper icon

Prompt

convert different pressure units to given unit

Answer

VBA Function for Pressure Unit Conversion

This document outlines a VBA function that converts various pressure units to a specified target unit. The function incorporates necessary libraries, has detailed documentation, validates input, and provides clear comments throughout.

Function Overview

The function ConvertPressure allows users to convert pressure values between five common units: Pascals, Atmospheres, Bar, Torr, and PSI.

Supported Units

  • Pascals (Pa)
  • Atmospheres (atm)
  • Bar
  • Torr (mmHg)
  • Pounds per Square Inch (PSI)

Function Code

Option Explicit

' Function to convert pressure values from one unit to another
' 
' @param pressureValue As Double: The pressure value to convert
' @param fromUnit As String: The unit of the pressure value (e.g., "Pa", "atm", "bar", "Torr", "PSI")
' @param toUnit As String: The unit to convert the pressure value to (e.g., "Pa", "atm", "bar", "Torr", "PSI")
' @return As Double: The converted pressure value in the target unit
' @raises Exception: If an invalid unit is specified
Function ConvertPressure(pressureValue As Double, fromUnit As String, toUnit As String) As Double
    Dim baseValue As Double
    
    ' Normalize the input units to Pascals for conversion
    Select Case LCase(fromUnit)
        Case "pa"
            baseValue = pressureValue
        Case "atm"
            baseValue = pressureValue * 101325 ' Convert Atmospheres to Pascals
        Case "bar"
            baseValue = pressureValue * 100000 ' Convert Bar to Pascals
        Case "torr"
            baseValue = pressureValue * 133.322 ' Convert Torr to Pascals
        Case "psi"
            baseValue = pressureValue * 6894.76 ' Convert PSI to Pascals
        Case Else
            Err.Raise vbObjectError + 1, "ConvertPressure", "Invalid 'from' unit specified."
    End Select
    
    ' Convert from Pascal to the target unit
    Select Case LCase(toUnit)
        Case "pa"
            ConvertPressure = baseValue
        Case "atm"
            ConvertPressure = baseValue / 101325 ' Convert Pascals back to Atmospheres
        Case "bar"
            ConvertPressure = baseValue / 100000 ' Convert Pascals back to Bar
        Case "torr"
            ConvertPressure = baseValue / 133.322 ' Convert Pascals back to Torr
        Case "psi"
            ConvertPressure = baseValue / 6894.76 ' Convert Pascals back to PSI
        Case Else
            Err.Raise vbObjectError + 2, "ConvertPressure", "Invalid 'to' unit specified."
    End Select
End Function

Documentation

Parameters

  • pressureValue (Double): The pressure value you want to convert.
  • fromUnit (String): The unit of the input pressure value.
  • toUnit (String): The unit to which you want to convert the pressure value.

Return Type

  • Returns a Double representing the converted pressure value.

Exceptions

  • Raises an exception if either the fromUnit or toUnit is invalid.

Example Usage

To use the ConvertPressure function, you can call it from another subroutine or an Excel cell. Here’s how you might invoke it:

Sub TestPressureConversion()
    Dim initialPressure As Double
    Dim convertedPressure As Double
    
    initialPressure = 1 ' 1 atm
    convertedPressure = ConvertPressure(initialPressure, "atm", "psi") ' Convert 1 atm to PSI
    
    Debug.Print "1 atm in PSI is: " & convertedPressure ' Output will be approximately 14.696 psi
End Sub

Explanation of Example

In the example above, we convert 1 Atmosphere to Pounds per Square Inch (PSI). The result will be printed in the Immediate Window in the VBA editor.

Conclusion

This function provides a clean, efficient method for converting pressure units in VBA. It is scalable for additional units and ensures robustness through input validation and error handling. For deeper insights and advanced topics, consider exploring courses on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document details a VBA function for converting various pressure units (Pascals, Atmospheres, Bar, Torr, PSI) with input validation and error handling, ensuring robust and efficient unit conversion in Excel.