PDA

View Full Version : How to assign values to the strings came from combo boxes on the basis of text..?



sabhay
11-22-2012, 12:29 PM
I am trying to a make unit converter in VBA using userform.
I am getting a value in combobox2 say "Degree" & a value in Combobox3 say "Radian"
In a function called calculate() , I want to do calculation & display result in textbox2

but before calculating I want to check & assign some numbers to combobox values. Can I do this..?????
following is part of my coding:

Private Sub calculation()
Dim Degree, Radian, Gradian As Double
If TextBox1 <> "" And ComboBox2 <> "" And ComboBox3 <> "" Then
If ComboBox2.Value = ComboBox3.Value Then TextBox2.Value = TextBox1.Value

Degree = 1
Radian = 1.11111
Gradian = 0.017453293

TextBox2.Value = ComboBox3 / ComboBox2


End If

End Sub

Bob Phillips
11-22-2012, 01:29 PM
What do you want to check for and what are those 3 values in the midst of the code supposed to do?

sabhay
11-22-2012, 01:54 PM
Suppose :
My value of ComboBox3 is " Degree" & ComboBox2 is "" Radian" .

I want them to become 1 & 1.11111 respectively :help

So that I will get the result : TextBox2.Value = 1 / 1.11111

please help me!!
Now I have attached the project file...: pray2:

Teeroy
11-22-2012, 11:51 PM
There were a number of things wrong with your code. You were trying to do calculations with strings (you need the Val function to turn a string into a number) and assign a conversion value based on variable name equivalence with the combobox. And conversion wise you had Gradians and Radians confused.

This will do what you've described (Remember it's only set up for angles though).

Private Sub calculation()
Dim Divisor As Double, Multiplier As Double

If TextBox1 <> "" And ComboBox2 <> "" And ComboBox3 <> "" Then

Select Case ComboBox2.Value
'to convert input value to degree equivalence
Case "Degree"
Divisor = 1
Case "Radian"
Divisor = 0.017453293
Case "Gradian"
Divisor = 1.11111
End Select

Select Case ComboBox3.Value
'to convert the degree equivalent figure to the output units
Case "Degree"
Multiplier = 1
Case "Radian"
Multiplier = 0.017453293
Case "Gradian"
Multiplier = 1.11111
End Select

TextBox2.Value = Val(TextBox1.Value) * Multiplier / Divisor
End If
End Sub