
Sub Prova()
Dim Selezione, A, B As Integer
Dim Risultato As Double
Selezione = Cells(1, 1)
A = Cells(2, 1)
B = Cells(3, 1)
If Selezione = 1 Then
Risultato = A + B
Cells(4, 1) = Risultato
Else
If Selezione = 2 Then
Risultato = A * B
Cells(4, 1) = Risultato
Else
If Selezione = 3 Then
Risultato = A / B
Cells(4, 1) = Risultato
Else
Cells(4, 1) = "Nessuna selezione valida"
End If
End If
End If
End Sub
|
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
Select Case Range("A1").Value
Case 1
Cells(4, 1) = Cells(2, 1) + Cells(3, 1)
Case 2
Cells(4, 1) = Cells(2, 1) * Cells(3, 1)
Case 3
Cells(4, 1) = Cells(2, 1) / Cells(3, 1)
Case Else
Cells(4, 1) = "Nessuna selezione valida"
End Select
End If
End Sub
|
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
Application.EnableEvents = False
Set r = Range("a2:a3")
On Error GoTo gest_err
With Application
r(3) = Switch(r(0) = 1, .Sum(r), _
r(0) = 2, .Product(r), _
r(0) = 3, r(1) / r(2), _
r(0) >= 0, "Nessuna selezione valida")
End With
Application.EnableEvents = True
Exit Sub
gest_err:
If Err.Number = 11 Then
r(3) = "Divisione per zero non ammessa"
Else
r(3) = "#Errore"
End If
Application.EnableEvents = True
End Sub |
