The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 3 of 3

Thread: VBA math triangle problem

  1. #1

    Afrostyle 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.
    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.TypeText Text:="The biggest side is: "
    Selection.TypeText Text:=max
    ' We show the sum of all the sides
    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.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.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.TypeText Text:="It exists such a triangle"
    ' it doesn't exist
    Selection.TypeText Text:="It doesn't exist"
    End If
    End Sub
    Please help me I was not able to find where the mistake is.

  2. #2
    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.


    If (s2 > CInt(max)) Then
    This gives me

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

  3. #3
    great answer, great explanation Now I can understand, 10x again

Thread Information

Users Browsing this Thread

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

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012