Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao barbiturico,
allora se non ci sono altre sorprese una soluzione potrebbe essere questa:
Option Explicit
Private Const mcMod = "Modulo2"
Private Const COL_DATA = 0
Private Const COL_ENTR = 1
Private Const COL_USCI = 2
Private Const COL_RIEC = 3
Private Const COL_DESC = 4
Private Const COL_DATA_NAME = "Data"
Private Const VAL_BEGEND = "Intervento"
Public Sub Test2()
Const cProc = "Test2"
If StrComp(Application.ActiveCell.Value _
, COL_DATA_NAME _
, vbTextCompare) Then
MsgBox "Questa macro deve essere avviata dalla cella " _
& "'" & COL_DATA_NAME & "'." _
, vbOKOnly + vbExclamation _
, cProc
Exit Sub
Else
Test2Execute Application.ActiveCell
End If
End Sub
Private Sub Test2Execute(ByVal rng As Excel.Range)
Const cProc = "Test2Execute"
On Error GoTo ErrH
Dim wsh As Excel.Worksheet
Dim rngData As Excel.Range
Dim rngEntr As Excel.Range
Dim rngUsci As Excel.Range
Dim rngRiEc As Excel.Range
Dim rngDesc As Excel.Range
Dim r As Long ' Row
Dim dtmDate As Date
Dim dblTot As Double
Dim blnEnd As Boolean
Dim blnTot As Boolean
Set wsh = rng.Parent
Do
r = r + 1
With rng
If IsEmpty(.Offset(r, COL_DATA).Value) Then
rngRiEc.Value = dblTot
Exit Do
End If
Set rngData = .Offset(r, COL_DATA)
Set rngEntr = .Offset(r, COL_ENTR)
Set rngUsci = .Offset(r, COL_USCI)
Set rngDesc = .Offset(r, COL_DESC)
If rngDesc.Value = VAL_BEGEND And rngEntr.Value <> "" Then
rngDesc.Font.Color = vbGreen
blnEnd = True
dtmDate = rngData.Value
Set rngRiEc = .Offset(r, COL_RIEC)
ElseIf rngDesc.Value = VAL_BEGEND Then
rngDesc.Font.Color = vbRed
If blnEnd Then blnTot = True
Else
If blnEnd And rngData.Value <> dtmDate Then blnTot = True
End If
If blnTot Then
rngRiEc.Value = dblTot
Set rngRiEc = Nothing
If rngEntr.Value <> "" _
Then dblTot = rngEntr.Value _
Else dblTot = -rngUsci.Value
blnEnd = False
blnTot = False
Else
If rngEntr.Value <> "" _
Then dblTot = dblTot + rngEntr.Value _
Else dblTot = dblTot - rngUsci.Value
End If
End With
Loop
ExitProc:
Set rngDesc = Nothing
Set rngRiEc = Nothing
Set rngUsci = Nothing
Set rngEntr = Nothing
Set rngData = Nothing
Set wsh = Nothing
Exit Sub
ErrH:
MsgBox "ERR#" & Err.Number & vbNewLine & Err.Description _
, vbCritical + vbOKOnly _
, mcMod & "." & cProc
Resume ExitProc
End Sub