PDA

View Full Version : [SOLVED:] Msg Box Problem



Sharism
12-07-2018, 06:22 AM
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

Paul_Hossler
12-07-2018, 03:11 PM
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

Sharism
12-07-2018, 03:16 PM
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

Paul_Hossler
12-07-2018, 05:20 PM
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

Sharism
12-07-2018, 05:48 PM
Thank you, I changed it