VBA if语句无法正常工作

时间:2018-06-14 12:57:12

标签: excel vba excel-vba

我正在尝试制作一个简单的Temp转换器,以便能够在华氏度,摄氏度和开尔文之间正确切换。代码并没有像它应该的那样工作。当我从华氏温度到凯尔文时,它告诉我它使用的代码是从华氏温度到摄氏温度而不是开尔文温度曲线。我甚至添加了msgbox来告诉我什么是什么。我还为我的组合框添加了选项。当使用" "作为字符串时,它喜欢忽略所有内容。我尝试过使用&And来实现此目的。

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   

这是我的组合框初始化的代码

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

我的宏定义如下:

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

3 个答案:

答案 0 :(得分:4)

您可以使用If Then ElseIf ...方法

来避免所有CallByName
Private Sub CommandButton10_Click()
    CallByName Me, ComboBox1.Value & "To" & ComboBox2.Value, VbMethod
End Sub

正确命名(并且更有意义......)你的惯例:

Sub FahrenheitToCelsius()
    TempConverter.TextBox2.Value = (TempConverter.Temp1.Value - 32) * (5 / 9)
    MsgBox "I am FtoC"
End Sub

Sub CelsiusToFahrenheit()
    TempConverter.TextBox2.Value = (TempConverter.Temp1.Value * 0.55555) + 32
    MsgBox "I am CtoF"
End Sub

Sub CelsiusToKelvin()
    TempConverter.TextBox2.Value = TempConverter.Temp1.Value + 273
    MsgBox "I am CtoK"
End Sub

Sub KelvinToCelsius()
    TempConverter.TextBox2.Value = TempConverter.Temp1.Value - 273
    MsgBox "I am KtoC"
End Sub

Sub FahrenheitToKelvin()
    TempConverter.TextBox2.Value = ((TempConverter.Temp1.Value - 32) * (5 / 9)) + 273
    MsgBox "I am FtoK"
End Sub

Sub KelvinToFarenheit()
    TempConverter.TextBox2.Value = ((TempConverter.Temp1.Value - 273) * (5 / 9)) + 32
    MsgBox "I am KtoF"
End Sub

答案 1 :(得分:1)

根据您的回复,我认为您的模块中没有Option Explicit

因此,您的条款FarenheitCelciusKelvin隐式定义,并且会采用默认值(例如“”或0)。所以你的比较并没有比较你的想法。

当你在单词周围加上“”时,你明确地将它们定义为常量字符串。所以现在你的代码可以干净地将ComboBox中的内容与你的选项进行比较。

为了掩盖我的一些评论,您可以在一个功能中解决您的计算问题。这是一个例子:

Function ConvertTemperatureUnit(TemperatureValue as Double, FromUnits as String, ToUnits as String) as Double
    Select Case FromUnits
        Case "Fahrenheit"
            Select Case ToUnits
                Case "Celcius"
                    ConvertTemperatureUnit = (TemperatureValue -32) * (5/9)
                Case "Kelvin"
                    ConvertTemperatureUnit = ((TemperatureValue - 32) * (5 / 9)) + 273.15 ' accuracy!
                Case Else
                    ConvertTemperatureUnit = TemperatureValue ' covers an error case that was not covered in your previous code!
        Case "Celcius"
            Select Case ToUnits
                Case "Fahrenheit"
                    ConvertTemperatureUnit = (TemperatureValue * 5/9) + 32 ' keep the form of formula consist - just a readability thing.
                Case "Kelvin"
                    ConvertTemperatureUnit = TemperatureValue + 273.15
                Case Else
                    ConvertTemperatureUnit = TemperatureValue
        Case "Kelvin"
            Select Case ToUnits
                Case "Fahrenheit"
                    ConvertTemperatureUnit = ((TemperatureValue - 273.15) * (5 / 9)) + 32
                Case "Celcius"
                    ConvertTemperatureUnit = TemperatureValue - 273.15
                Case Else
                    ConvertTemperatureUnit = TemperatureValue
        Case Else
            ConvertTemperatureUnit = TemperatureValue ' soft fail.
    End Select

End Function

感谢@Peh,这是另一个例子:

Function ConvertTemperatureUnit(TemperatureValue as Double, FromUnits as String, ToUnits as String) as Double
    Select Case FromUnits & "To" & ToUnits  ' the added "To" makes it human readable
        Case "FahrenheitToCelcius"
            ConvertTemperatureUnit = (TemperatureValue -32) * (5/9)
        Case "FahrenheitToKelvin"
            ConvertTemperatureUnit = ((TemperatureValue - 32) * (5 / 9)) + 273.15
        Case "CelciusToFahrenheit"
            ConvertTemperatureUnit = (TemperatureValue * 5/9) + 32 
        Case "CelciusToKelvin"
            ConvertTemperatureUnit = TemperatureValue + 273.15
        Case "KelvinToCelcius"
            ConvertTemperatureUnit = TemperatureValue - 273.15
        Case "KelvinToFahrenheit"
            ConvertTemperatureUnit = ((TemperatureValue - 273.15) * (5 / 9)) + 32
        Case Else
            ConvertTemperatureUnit = TemperatureValue ' soft fail.
    End Select

End Function

答案 2 :(得分:0)

我试图在不同的温度范围内多次添加" "并且没有任何工作。我不确定为什么,但这次有用了。我仍然很困惑,但它现在有效。谢谢你的帮助。

    If ListFromUnit.Value = "Fahrenheit" And ListToUnit.Value = "Celsius" Then
        Call FtoC
    ElseIf ListFromUnit.Value = "Celsius" And ListToUnit.Value = "Fahrenheit" Then
         Call CtoF
    ElseIf ListFromUnit.Value = "Celsius" And ListToUnit.Value = "Kelvin" Then
         Call CtoK
    ElseIf ListFromUnit.Value = "Kelvin" And ListToUnit.Value = "Celsius" Then
         Call KtoC
    ElseIf ListFromUnit.Value = "Fahrenheit" And ListToUnit.Value = "Kelvin" Then
         Call FtoK
    ElseIf ListFromUnit.Value = "Kelvin" And ListToUnit.Value = "Fahrenheit" Then
         Call KtoF
    End If