
Option Explicit
Sub frequenza_vfrac()
Dim cell As Range
Dim media_positivi As Single, media_negativi As Single
Dim i As Integer, j As Integer
Dim positivi As Collection, negativi As Collection
Dim v As Variant, media_pos As Single, media_neg As Single
Set positivi = New Collection
Set negativi = New Collection
For Each cell In [c1:c20]
If Sgn(cell) > 0 Then
i = i + 1
j = 0
Else
j = j + 1
i = 0
End If
If Sgn(cell.Offset(1)) <> Sgn(cell) Then
If i > 0 Then positivi.Add i
If j > 0 Then negativi.Add j
i = 0
j = 0
End If
Next
For Each v In positivi
media_pos = media_pos + v
Next
For Each v In negativi
media_neg = media_neg + v
Next
media_pos = media_pos / positivi.Count
media_neg = media_neg / negativi.Count
MsgBox "Media frequenza dei consecutivi positivi = " & media_pos & vbCrLf & "Media frequenza dei consecutivi positivi = " & media_neg
End Sub |
If positivi.Count > 0 Then media_pos = media_pos / positivi.Count
If negativi.Count > 0 Then media_neg = media_neg / negativi.Count
Option Explicit
Sub frequenza2_vfrac()
Dim cell As Range, positivi As Range, negativi As Range
Dim m As Single
Dim media_pos As Single, media_neg As Single
On Error Resume Next
For Each cell In [C1:C20]
If Sgn(cell) > 0 Then
Set positivi = Union(positivi, cell)
Else
Set negativi = Union(negativi, cell)
End If
Next
For Each cell In positivi.Areas
m = m + cell.Count
Next
If m > 0 Then media_pos = m / positivi.Areas.Count
m = 0
For Each cell In negativi.Areas
m = m + cell.Count
Next
If m > 0 Then media_neg = m / negativi.Areas.Count
MsgBox "Media frequenza dei consecutivi positivi = " & media_pos & vbCrLf & "Media frequenza dei consecutivi negativi = " & media_neg
End Sub
Private Function Union(Rng1 As Range, Rng2 As Range) As Range
'thx to Tushar Mehta
'dailydoseofexcel.com/archives/2007/08/17/two-new-range-functions-union-and-subtract/
If Rng1 Is Nothing Then
Set Union = Rng2
ElseIf Rng2 Is Nothing Then
Set Union = Rng1
Else
Set Union = Application.Union(Rng1, Rng2)
End If
End Function
|
Sub freqSgnNumb()
Dim cell As Range, SumEachFrq As Integer, EachFrq As Integer, i As Integer, frqColl As New Collection
On Error Resume Next
For Each cell In [c1:c20]
If IsNumeric(cell) And cell <> "" Then
If Sgn(cell) = Sgn(cell.Offset(1)) Then
EachFrq = EachFrq + 1
Else
frqColl.Add (EachFrq + 1): EachFrq = 0
End If
End If
Next
Do
i = i + 1
SumEachFrq = SumEachFrq + frqColl(i)
Loop Until i = frqColl.Count
MsgBox "La persistenza media del segno numeri è: " & Round(SumEachFrq / frqColl.Count, 2)
End Sub
|
