-
Hi there,
I have 3 textboxes and a command button. When the button is clicked, the result of text2 divided by text1 appears in text3.
Decimals are not allowed - has to be fractions or whole numbers. I've got it working pretty well:
Private Sub Command1_Click()
If Text1 Mod Text2 > 0 Then
Text3 = Text1 & "/" & Text2
Else:
Text3 = Text1 / Text2
End If
End Sub
However, how can I have it so that the answer is simplified? For example, 12 in text1 and 8 in text2 will give 12/8 in text3, but it would be so much better if it simplified it automatically to 3/2.
Any ideas?
Thanks for any help
-
Hi,
For No Decimals Try: "\"
Text3 = Text1 & "\" & Text2
Sorry about the fractions!
Good Luck.
-
try this
Code:
Dim text1 As Integer, text2 As Integer, i As Integer, HighDivNumber As Integer
'since we know text1 will be bigger than text2 this is how to do it
'you will need to write your own statement to make sure text1 is bigger
text1.Text = text1
text2.Text = text2
For i = 1 To text2
If (text1 Mod i) = 0 And (text2 Mod i) = 0 Then
HighDivNumber = i
End If
Next i
text1 = text1 / HighDivNumber
text2 = text2 / HighDivNumber
'now put these numbers in your 3rd text box
I haven't verified if it works, but I think it will.