Foglio1

Private Sub CommandButton1_Click() Dim x As Complesso x = LeggiCellaComplessa("B6") Range("C14").Value = x.Re Range("C15").Value = x.Im Range("C16").Value = x.Mo Range("C17").Value = x.Ph End Sub



Modulo1

Type Complesso Re As Double Im As Double Mo As Double Ph As Double End Type Function LeggiCellaComplessa(cella As String) As Complesso stringa = Range(cella).Value S = Split(stringa, "exp(i") ' 2 exp( i3) --> S(0)=" 2 " , S(1)=" 3 )" If S(0) <> stringa Then ' se nella stringa compare "exp(i" S(0) = Trim(S(0)) ' S(0)="2" S(1) = Trim(S(1)) ' S(1)="3)" S(1) = Left(S(1), Len(S(1)) - 1) ' S(0)="3 "; S(1) = Trim(S(1)) ' S(1)="3" LeggiCellaComplessa.Mo = S(0) LeggiCellaComplessa.Ph = S(1) LeggiCellaComplessa.Re = S(0) * Cos(S(1)) LeggiCellaComplessa.Im = S(0) * Sin(S(1)) Else S = Split(stringa, "i") ' 2 + i 3 --> S(0)=" 2 + " , S(1)=" 3 " If S(0) <> stringa Then ' se nella stringa compare "i" S(0) = Trim(S(0)) ' S(0)="2 +" segno = Right(S(0), 1) ' segno="+"; S(0) = Left(S(0), Len(S(0)) - 1) ' S(0)="2 "; S(0) = Trim(S(0)) ' S(0)="2"; S(1) = Trim(S(1)) ' S(1)="3" If segno = "-" Then S(1) = -1 * S(1) End If LeggiCellaComplessa.Re = S(0) LeggiCellaComplessa.Im = S(1) LeggiCellaComplessa.Mo = Sqr(S(0) ^ 2 + S(1) ^ 2) If S(0) >= 0 Then LeggiCellaComplessa.Ph = Atn(S(1) / S(0)) Else LeggiCellaComplessa.Ph = Atn(S(1) / S(0)) + Pi End If Else LeggiCellaComplessa.Re = 0 LeggiCellaComplessa.Im = 0 LeggiCellaComplessa.Mo = 0 LeggiCellaComplessa.Ph = 0 End If End If End Function


LeggiCellaComplessa.xls