I am trying to make a simple Temp converter to be able to correctly switch between Fahrenheit, Celsius, and Kelvin. The code isn't working the way it's supposed to. When I go from Fahrenheit to Kelvin it tells me it's using code to go from Fahrenheit to Celsius not Kelvin. I even added msgbox to tell me what is pulling what. I have also added the options for my combo boxes. It likes to ignore everything when using " " as a string. I have tried using both & and And to get this to work.
Private Sub CommandButton10_Click()
If ComboBox1.Value = Fahrenheit & ComboBox2.Value = Celsius Then
Call FtoC
ElseIf ComboBox1.Value = Celsius & ComboBox2.Value = Fahrenheit Then
Call CtoF
ElseIf ComboBox1.Value = Celsius & ComboBox2.Value = Kelvin Then
Call CtoK
ElseIf ComboBox1.Value = Kelvin & ComboBox2.Value = Celsius Then
Call KtoC
ElseIf ComboBox1.Value = Fahrenheit & ComboBox2.Value = Kelvin Then
Call FtoK
ElseIf ComboBox1.Value = Kelvin & ComboBox2.Value = Fahrenheit Then
Call KtoF
End If
End Sub
This is the code for initialize for my comboboxes
Private Sub UserForm_Initialize()
Call CallSetting
'change position
Me.StartUpPosition = 0
Me.Top = (Application.Height / 2) - Me.Height / 2
Me.Left = (Application.Width / 2) - Me.Width / 2
With ComboBox1
.AddItem ("Fahrenheit")
.AddItem ("Celsius")
.AddItem ("Kelvin")
End With
With ComboBox2
.AddItem ("Fahrenheit")
.AddItem ("Celsius")
.AddItem ("Kelvin")
End With
TextBox2.Locked = True
End Sub
My macros are defined as the following:
Sub FtoC()
TempConverter.TextBox2.Value = (TempConverter.Temp1.Value - 32) * (5 / 9)
MsgBox "I am FtoC"
End Sub
Sub CtoF()
TempConverter.TextBox2.Value = (TempConverter.Temp1.Value * 0.55555) + 32
MsgBox "I am CtoF"
End Sub
Sub CtoK()
TempConverter.TextBox2.Value = TempConverter.Temp1.Value + 273
MsgBox "I am CtoK"
End Sub
Sub KtoC()
TempConverter.TextBox2.Value = TempConverter.Temp1.Value - 273
MsgBox "I am KtoC"
End Sub
Sub FtoK()
TempConverter.TextBox2.Value = ((TempConverter.Temp1.Value - 32) * (5 / 9)) + 273
MsgBox "I am FtoK"
End Sub
Sub KtoF()
TempConverter.TextBox2.Value = ((TempConverter.Temp1.Value - 273) * (5 / 9)) + 32
MsgBox "I am KtoF"
End Sub
&to the actual wordAnd. Ampersand is not recognizable syntax for logic operators.ComboBox1.Value = FahrenheitisFahrenheita variable? then declare it properly. If it is a string then useComboBox1.Value = "Fahrenheit". I recommend to useOption Explicit.Option Explicitat the top of each code module will ensure you have proper sytnax and variable declaration in your code (and save you a lot of troubleshooting). To set this automatically, in the VBE, go toTools > Options > Editor > Require Variable Declarations.ComboBox1are completely meaningless and hard to understand. A better practice would be to name them meaningful likecmbFromUnitandcmbToUnit. If you use meaningful object and variable names you make life much easier for yourself (especially as a beginner). • Apply all these comments here and your issue is solved.