# Thread: VBA math triangle problem

1. 132
posts
Registered User

## VBA math triangle problem

Hi all . I faced up a problem. I have written this code, but it doesn't work. No matter what values I enter it shows me that Such triangle doesn't exist. The point is to create a program that shows the biggest side and tells us whether the triangle exists or not. I am new in VBA.
Code:
```Sub example()
Dim Message, Title, Default, MyValue
Dim s(3)
max = 0 ' the biggest side
sum = 0 ' sum of all the sides
s2 = 0 ' sum of the other 2 sides
For i = 1 To 3
Message = "Enter value:"
Title = "Side Values"
Default = "0"
MyValue = InputBox(Message, Title, Default)
' we put MyValue into the array
s(i) = MyValue
' here we find which of the sides is the biggest
If (s(i) > max) Then
max = s(i)
End If
' we sum all the sides
sum = sum + s(i)
Next i
' we show the biggest one
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeText Text:="The biggest side is: "
Selection.TypeText Text:=max
' We show the sum of all the sides
Selection.TypeParagraph
Selection.TypeText Text:="Sum of all the sides is: "
Selection.TypeText Text:=sum
' here we find the sum of the other 2 sides(not including the max)
s2 = sum - max
' we show the sum of the 2 sides
Selection.TypeParagraph
Selection.TypeText Text:="Sum of the two sides: "
Selection.TypeText Text:=s2
'Again we show the max just to be sure it is the same
Selection.TypeParagraph
Selection.TypeText Text:=max
' We check whether a triangle exists
' if it exists then its max side must be smaller than the sum of the other two
If (s2 > max) Then
' it exists
Selection.TypeParagraph
Selection.TypeText Text:="It exists such a triangle"
Else
' it doesn't exist
Selection.TypeParagraph
Selection.TypeText Text:="It doesn't exist"
End If
End Sub```

2. 1,627
posts
hugeExplosions = true;
Your mistake is that you havent given a type to the s variable

dim s(3)

An integer declaration will help here

The reason for this is, without a type declaration the variable is considered a 'Variant'

When you input your lengths in the input box they are taken as string values and then stored in the variant. When you use + for the sum = sum + s the compiler sees that you are using the addition + function and therefore converts the value of sum and s into an integer

When you check if s2 > max you are checking if an integer is greater than a string as your max variable is untyped and your code gives the compiler no hints as to what type the variable should be, so it remains a string

You can use CInt(max) to convert max to an int or you can type your variables. Either one should work.

e.g.

Code:
```If (s2 > CInt(max)) Then
code...```
This gives me

The biggest side is: 5
Sum of all the sides is: 15
Sum of the two sides: 10
5
It exists such a triangle

3. 132
posts
Registered User
great answer, great explanation Now I can understand, 10x again

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•