Small code for temperature converter....

albertc30

Well-known Member
Joined
May 7, 2012
Messages
1,091
Office Version
  1. 2019
Platform
  1. Windows
Hello everybody.

I got this video on youtube and decided to have a go as it seems quite simple.

This will give me an insite how the variables are played about and oerhaps I can understand the logic of all this better.

The code I have seen is in actuall VB express 2010 so I have done my very best to adapt this to VBA excel 2013.

But sadly not all of it works okay.

My code is as follows;

Code:
Private Sub CommandButton1_Click()    Dim Celsius As String
    Dim Farenheit As String
    Dim Answer As String
    
[COLOR=#ff0000]    Celsius = texbox1.Text[/COLOR]
[COLOR=#ff0000]    Farenheit = texbox2.Text[/COLOR]
    
    If (TextBox1.Text & TextBox2.Text = vbnullorempty) Then
        MsgBox "Please enter a value on either Celsius or Farenheit box.", vbCritical, "Error"
        Else
            If (TextBox1.Text = vbnullorempty) Then
[COLOR=#ff0000]                Answer = Celsius * 9 / 5 + 32[/COLOR]
                TextBox2.Text = Int(Answer)
            End If
            If (TextBox2.Text = vbnullorempty) Then
[COLOR=#ff0000]                Answer = (Farenheit - 32) * 5 / 9[/COLOR]
                TextBox2.Text = Int(Answer)
            End If
    End If
End Sub

The lines in red are the ones giving me a hard time. I know that the Answer = line is wrong when I have the first two in red as just comments, therefore bypassing them to the next line of code.

Any help as always very much appreciated.

Regards,

Albert
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You've Dim'd Answer as a string and then trying to pass a numerical value to it, Answer can only hold numerical values. Try Dim Answer as Long and see if that helps

You probably should convert Celsius and Farenheit into numerical values too, i.e. Answer = Val(Celsius) * 9 / 5 + 32
 
Last edited:
Upvote 0
Done that but now flags Answer = (Farenheit - 32) * 5 / 9 or Answer = Celsius * 9 / 5 + 32 as wrong...
 
Upvote 0
Wrong meaning what is the error message given? Are all your data types consistent, i.e. you want a numerical answer, are you providing numerical values to your equation?
 
Upvote 0
This is what i got now but still flags those first two lines in red...

Mate, I am learning this, I am a complete rookie... just trying to make sense of the code and it' structure.

Code:
Private Sub CommandButton1_Click()
    Dim Celsius As String
    Dim Farenheit As String
    Dim Answer As Long
    
    [COLOR=#ff0000]Celsius = texbox1.Text
    Farenheit = texbox2.Text[/COLOR]
    
    If (TextBox1.Text & TextBox2.Text = vbnullorempty) Then
        MsgBox "Please enter a value on either Celsius or Farenheit box.", vbCritical, "Error"
        Else
            If (TextBox1.Text = vbnullorempty) Then
                [COLOR=#ff0000]Answer = Val(Celsius) * 9 / 5 + 32[/COLOR]
                TextBox2.Text = Int(Answer)
            End If
            If (TextBox2.Text = vbnullorempty) Then
                [COLOR=#ff0000]Answer = Val(Farenheit - 32) * 5 / 9[/COLOR]
                TextBox2.Text = Int(Answer)
            End If
    End If
End Sub
 
Upvote 0
Had to modify your code to work for me as I do not know the userform or TextBox you're using but run this in a separate workbook and try to adjust it:
Code:
Sub Test()
    Dim Celsius As Long
    Dim Farenheit As Long
    Dim Answer As Long
    
    On Error Resume Next
    Celsius = InputBox("Enter C")
    Farenheit = InputBox("Enter F")
    On Error GoTo 0
    
    If Celsius = vbNull Then Celsius = 0
    If Farenheit = vbNull Then Farenheit = 0
    
    If Not IsNumeric(Celsius + Farenheit) Then
        MsgBox "Please enter a value on either Celsius or Farenheit box.", vbCritical, "Error"
    Else
        If Celsius = 0 Then Answer = Val(Celsius) * 9 / 5 + 32
        If Farenheit = 0 Then Answer = Val(Farenheit - 32) * 5 / 9
        MsgBox Answer
    End If
End Sub
 
Upvote 0
Sorry but I forgot to mention that I am using a userForm1.

The way you have written it it is showing a popup dialog asking for data.
 
Upvote 0
Had to modify your code to work for me as I do not know the userform or TextBox you're using but run this in a separate workbook and try to adjust it

See first line in previous reply
 
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