VBA Custom Function Celsius to Fahrenheit and vice versa

brian662

New Member
Joined
Feb 8, 2013
Messages
6
Hi, I have two functions, one called CtoF and the other called FtoC. As you might expect, one function converts temperatures from Centigrade to Fahrenheit, the other from Fahrenheit to Centrigrade.

The formulas I need to be able to do the conversion are:

Tc = (5/9)*(Tf-32)
Tf = (9/5)*Tc+32

(Tc = temperature in degrees Celsius, Tf = temperature in degrees Fahrenheit)

I am looking to write the code so that you can check for invalid data and perform other actions before carrying out the conversion.

I know to get started, I will probably want to place a function declaration like

Function CtoF(sngFar As Single) As Single
End Function

But where do I go from here??

I would much appreciate the help
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try:
Code:
Function CtoF(sngFar As Single) As Single
    On Error Goto ErrorHandler
    CtoF = (9 / 5) * sngFar + 32
    Exit Function
ErrorHandler:
    'Replace with whatever you want to happen when it errors.
    Msgbox "ERROR!"
    CtoF = CVErr(xlErrValue)
End Function
 
Upvote 0
You might try the built in function called CONVERT

=CONVERT(A1,"C","F") <-Celcius to Fahrenheit
=CONVERT(A1,"F","C") <-Fahrenheit to Celcius
 
Upvote 0

Forum statistics

Threads
1,225,882
Messages
6,187,579
Members
453,430
Latest member
Heric

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