Home > other >  VBA Code: How to convert Celsius to Fahrenheit and vice versa with a MsgBox and InputBox
VBA Code: How to convert Celsius to Fahrenheit and vice versa with a MsgBox and InputBox

Time:10-07

  • My problem is converting the input box value into either Celsius or Fahrenheit based on either "C", "c", or "F", "f" at the end of the number. For example if the input user puts 32f or 32F the message box displays 0 C and vice versa.

  • Here is my code:

Sub Conversion()
    Dim temp As String
    temp = Val(InputBox("Enter the Temperature followed by F for Fahrenheit or C for Celsius"))
    
    Dim f As String
    Dim c As String
    Dim Conversion As String
    
    f = ((9 * temp) / (5   32))
    c = ((temp - 32) * (5 / 9))
    
    Conversion = InStr(1, temp, "c")

    
    If temp = True Then
    
        f = ((9 * temp) / (5   32))
        MsgBox ("Temperature in F is" & f)

    Else
        c = ((temp - 32) * (5 / 9))
        MsgBox ("Temperature in C is " & c)

    
    End If
End Sub

CodePudding user response:

As per the comments

  1. f and c should be doubles.
  2. Val will remove the text on which you are trying to test. so create a new variable to hold the number.
  3. Make sure you use vbTextCompare to ensure match regardless of case.
  4. Test Conversion not temp
  5. The formula for Fahrenheit had () in the wrong place.
Sub Conversion()
    Dim temp As Variant
    temp = InputBox("Enter the Temperature followed by F for Fahrenheit or C for Celsius")
    
    Dim tempNum As Double
    tempNum = Val(temp)
    
    Dim f As Double
    Dim c As Double
    Dim Conversion As String
    Conversion = InStr(1, temp, "c", vbTextCompare)

    If Conversion > 0 Then
        f = ((9# * tempNum / 5)   32)
        MsgBox "Temperature in F is" & f
    Else
        c = ((tempNum - 32#) * (5 / 9))
        MsgBox "Temperature in C is " & c
    End If
End Sub

CodePudding user response:

There is all sorts of issues with the code you posted. An example of using Ucase, Left, Mid and Right is below.

Sub Conversion()
    Dim temp As String
    Dim retVal As String
    
    temp = InputBox("Enter the Temperature followed by F for Fahrenheit or C for Celsius")
    
    If (UCase(Right(Trim(temp), 1))) = "C" Then
        retVal = "Temperature in C is " & ((9 * CInt(Left(temp, Len(temp) - 1))) / 5 )  32)
    
    ElseIf (UCase(Right(Trim(temp), 1))) = "F" Then
        
        retVal = "Temperature in F is " & ((CInt(Left(temp, Len(temp) - 1)) - 32) * (5 / 9))
    Else
        retVal = "Could not determine Fahrenheit or Celsius for the input value: " & temp
    End If

    MsgBox retVal
End Sub

CodePudding user response:

Your code isn't the main problem; your calculation is off.

Conversion from Celsius to Farenheit is (n * 9/5) 32. You're doing (n*9)/(5 32).

You're also excluding the text portion of the result right off the bat; the unit being selected isn't stored anywhere.

Sub ConversionV2()
    Dim strInput As String
    Dim dblTemp As Double

    strInput = InputBox("Enter the Temperature followed by F for Fahrenheit or C for Celsius")
    dblTemp = Val(strInput)
    
    Select Case UCase(Right(strInput, 1))
        Case Is = "C"
            MsgBox ("Temperature in F is " & ((dblTemp * 9) / 5)   32)
        Case Is = "F"
            MsgBox ("Temperature in C is " & ((dblTemp - 32) * 5) / 9)
        Case Else
            MsgBox "Sorry, I didn't recognise that input"
    End Select
End Sub

Storing the whole input as a string allows us to perform the operations more easily. We can pull the VAL out of the string, and we can look at the last character to see what conversion to use. Additionally, using a Select statement lets us control for more than just the two expected results.

It's also bad practice to carry out calculations you don't need; we don't need to store both F and C if we're only going to be using one of them. Putting the calculation in the Select statement ensures that only the necessary calculation is carried out.

  • Related