Consulting

Results 1 to 5 of 5

Thread: Msg Box Problem

  1. #1
    VBAX Regular
    Joined
    May 2016
    Posts
    21
    Location

    Msg Box Problem

    Hello guys,

    just wanted to as if you could help me.

    • Your macro will do some basic calculation for rectangles
    • First the user enters length and width of the triangle
    • Then the user enters what kind of calculation has to be done
    1: circumfence
    2: area
    3: the length of the diagonal
    • If the user chooses an invalid calculation a message has to be displayed
    • Show the result of the calculation in a message box

    Private Sub Worksheet_Change(ByVal Target As Range)
    
       If Target.Address(0, 0) = "G12" Then
          Select Case Target.Value
             Case "circumfence"
                Range("H14").FormulaR1C1 = "=2*(R[-2]C[-6]+R[-1]C[-6])"
             Case "area"
                Range("H14").FormulaR1C1 = "=R[-2]C[-6]*R[-1]C[-6]"
             Case "diagonal"
                Range("H14").FormulaR1C1 = "=SQRT(SUMSQ(R[-2]C[-6]:R[-1]C[-6]))"
             Case Else
                Range("H14").Value = ""
          End Select
       End If
    
    
    End Sub

    I did it like this, but it is false because there is no message box. So I wanted to know, how I transform this into the message box?

    Many thanks


    Last edited by Sharism; 12-07-2018 at 06:44 AM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Not tested but something like this will display a MsgBox

    Private Sub Worksheet_Change(ByVal Target As Range)
       If Target.Address(0, 0) = "G12" Then
          Select Case Target.Value
             Case "circumfence"
               Range("H14").FormulaR1C1 = "=2*(R[-2]C[-6]+R[-1]C[-6])"
             Case "area"
                Range("H14").FormulaR1C1 = "=R[-2]C[-6]*R[-1]C[-6]"
             Case "diagonal"
                Range("H14").FormulaR1C1 = "=SQRT(SUMSQ(R[-2]C[-6]:R[-1]C[-6]))"
             Case Else
                Range("H14").Value = ""
          End Select
    
          If Len(Range("H14")) = 0 then
      Msgbox "Bad Calculation"  
          Else
                  MsgBox Range("H14").Value
          Endif
       End If
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    May 2016
    Posts
    21
    Location
    Hello Paul,

    I rewrote it like this and it worked:

    Sub Rectangle()
    
    'define the variables
    Dim circumfence As Double
    Dim area As Double
    Dim diagonal As Double
    
    
        Dim l As Integer
        Dim w As Integer
    
    
        'select where the variables are
        l = Range("B1").Value
        w = Range("B2").Value
    
    
            'defining the formulas
            If Range("B3").Value = 1 Then
     
                    circumfence = (2 * l) + (2 * w)
                
                    MsgBox "The Circumfence is:" & circumfence
     
                ElseIf Range("B3").Value = 2 Then
    
    
                    area = l * w
        
                    MsgBox "The area is:" & area
         
    
    
                
                ElseIf Range("B3").Value = 3 Then
    
    
                    diagonal = Sqr(l ^ 2 + w ^ 2)
        
                    MsgBox "The diagonal is:" & diagonal
     
                'If you dont select 1,2 or 3
                Else
         
                MsgBox "Please choose the right calculation", vbCritical, "Warning"
    
    
         
            End If
     
    End Sub

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    OK

    Mark it solved - #3 in my signature


    I'd Dim l and w as Double.

    A number in the cell would be a Double, and should someone enter something like 13.75 the calculations would be off since it would be treated like 13.0

    Dim l As Integer Dim w As Integer 'select where the variables are l = Range("B1").Value w = Range("B2").Value
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    May 2016
    Posts
    21
    Location
    Thank you, I changed it

Posting Permissions

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