Temperature Convertor

sliollio

New Member
Joined
Sep 8, 2014
Messages
22
I need help writing a code that will:

  1. Ask the user for input temperature (numerical value) and temperature type (Celsius/Fahrenheit).
  2. Convert the temperature to Fahrenheit/Celsius depending on the input. If the given temperature is in Fahrenheit, convert it to Celsius and vice-versa.
  3. Display the result in a message box.

I know the basic formulas for converting from one to another:
F = ((9/5)*C) + 32
C = (5/9)*(F - 32)

Just not sure how you would go about having the program determine whether or not the user inputted a temp in C or F and then how to get it to choose the correct formula.:confused:

Sub TempConvertor()
Dim Temp As Double
Dim F As Integer
Dim C As Integer
Temp = InputBox("Enter a temperature as well as F (for fahrenheit) or C (for Celcius)")
F = ((9 / 5) * C) + 32
C = ((5 / 9) * (F - 32))
MsgBox (Temp)
End Sub


There's where I am at so far, which I know is way off:(
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this..

Enter just ##@ in the input box
Number followed immediately by either F or C, no spaces
72F
16C

Code:
Sub TempConvertor()
Dim Temp As String
Dim F As Double
Dim C As Double
Dim T As Double

Start:
Temp = InputBox("Enter a temperature as well as F (for fahrenheit) or C (for Celcius)", "Temperature", "72F")
T = Left(Temp, Len(Temp) - 1)
Select Case UCase(Right(Temp, 1))
    Case "C"
        F = ((9 / 5) * T) + 32
        MsgBox F
    Case "F"
        C = ((5 / 9) * (T - 32))
        MsgBox C
    Case Else
        MsgBox "Invalid entry, try again"
        GoTo Start
End Select
End Sub
 
Upvote 0
Another option
Enter Value for conversion & "C" or "F" , position not important:-
Results in msgbox
Code:
[COLOR=Navy]Sub[/COLOR] MG10Oct48
[COLOR=Navy]Dim[/COLOR] Temp [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
[COLOR=Navy]Dim[/COLOR] tVal [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
Temp = InputBox("Enter a temperature as well as F (for fahrenheit) or C (for Celcius)")
    [COLOR=Navy]If[/COLOR] InStr(UCase(Temp), "C") > 0 [COLOR=Navy]Then[/COLOR]
        Temp = Replace(Temp, "C", "")
        tVal = Val(Temp)
        MsgBox "Temp F = " & ((9 / 5) * tVal) + 32 & "°F"
    [COLOR=Navy]ElseIf[/COLOR] InStr(UCase(Temp), "F") > 0 [COLOR=Navy]Then[/COLOR]
        Temp = Replace(UCase(Temp), "F", "")
        tVal = Val(Temp)
        MsgBox "Temp C =" & ((5 / 9) * (tVal - 32)) & "°C"   
[COLOR=Navy]    End[/COLOR] [COLOR=Navy]If[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top