Unit conversion with VBA in Microsoft Excel

Die Worksheet-Funktion Convert() bietet eine große Anzahl an Umrechnungsmöglichkeiten für physikalische Einheiten. Es gibt sie erst seit Excel 2007. Dabei können Sie auch Einheiten mit Präfix nutzen, wie z. B. das „k“ für den Faktor 1.000 wie bei „km“. Die folgenden vier Zahlenwerte (Angaben für Entfernung, Energie, Temperatur und Druck) werden in eine andere Einheit umgerechnet. Gleichzeitig werden sie zur deutlicheren Darstellung, zusammen mit den Ergebnissen der Umrechnung, formatiert:

Sub Umrechnungen()
ThisWorkbook.Worksheets("Tabelle2").Activate
' Entfernung
Range("A2").Value = Application.WorksheetFunction. _
Convert(Range("A1").Value, "km", "mi")
Range("A1").NumberFormatLocal = "0,000 ""Km"""
Range("A2").NumberFormatLocal = "0,000 ""Miles"""
' Energie
Range("A5").Value = Application.WorksheetFunction. _
Convert(Range("A4").Value, "J", "cal")
Range("A4").NumberFormatLocal = "0,00 ""J"""
Range("A5").NumberFormatLocal = "0,000 ""cal"""
' Temperatur
Range("A8").Value = Application.WorksheetFunction. _
Convert(Range("A7").Value, "C", "F")
Range("A7").NumberFormatLocal = "0,0 ""Grad C"""
Range("A8").NumberFormatLocal = "0,0 ""Grad F"""
' Druck
Range("A11").Value = Application.WorksheetFunction. _
Convert(Range("A10").Value, "hPa", "mmHg")
Range("A10").NumberFormatLocal = "0,000 ""hPa"""
Range("A11").NumberFormatLocal = "0,000 ""mm HG"""
End Sub

Meters to Miles: Im ersten Fall wird eine Entfernungsangabe aus Metern in britische Meilen umgerechnet. Dazu sind die Angaben km und mi in Parameter 2 und 3 notwendig. Der Präfix k sorgt dafür, dass der Wert in Kilometern angenommen wird.

Joules to Calories: In the second case, an energy specification is converted from joules to calories. For this, the information J and cal in parameters 2 and 3 are necessary.

Celsius to Fahrenheit: In the third case, a temperature specification from degrees Celsius (C) is converted into degrees Fahrenheit (F).

Air pressure: Die letzte Umrechnung ermittelt eine (Luft-)Druckangabe in mm Quecksilbersäule ( mmHg ) aus Hektopascal ( hPa ). Das h ist dabei der Präfix für Hekto, also hundert.

Text in the format: Alle Zellen wurden passend formatiert. Zur Erinnerung: Eine Textangabe innerhalb eines Formats muss in zweifachen doppelten Anführungsstrichen stehen.

Was the explanation to "Unit conversion with VBA in Microsoft Excel"Helpful? Rate now:

More explanations too