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
 
Albertc30,

I downloaded your file, and first thing I noticed with your code is

Code:
Private Sub CommandButton1_Click()
    Dim Celsius As String
    Dim Farenheit As String
    Dim Answer As Long
    
    Celsius = TexBox1.Text
    Farenheit = TexBox2.Text

You have the variable textbox spelled wrong.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I have changed it but still to no avail.

This is doing my head in guys...

What am i doing wrong?

Cheers.

Albert
 
Upvote 0
Have corrected your mathematical errors and references and changed datatypes. Replace your form code with this:-

Code:
Private Sub CommandButton1_Click()
    Dim Celsius As Long
    Dim Farenheit As Long
    Dim Answer As Long
    
    Celsius = Val(TextBox1.Text)
    Farenheit = Val(TextBox2.Text)
    
    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
                Answer = Val(Farenheit - 32) * 5 / 9
                TextBox1.Text = Int(Answer)
            End If
            If (TextBox2.Text = vbnullorempty) Then
                Answer = Val(Celsius) * 9 / 5 + 32
                TextBox2.Text = Int(Answer)
            End If
    End If
End Sub
 
Upvote 0
Thank you mate.

I shall have a read through the code and see what are my findings and understanding of the code.

Regards,
Albert
 
Upvote 0

Forum statistics

Threads
1,225,889
Messages
6,187,678
Members
453,435
Latest member
U4US

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