Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws2 As Worksheet
Dim area As Range
Dim cl As Range
Dim anno As Integer
Dim tabella As Range
Dim riga As Long
Dim firstAddress As String
Dim tot As Integer
Set ws2 = ThisWorkbook.Sheets("Inserimento Spese")
anno = Me.Range("B1")
Set area = ws2.Range("A5:A20")
Set tabella = Me.Range("A8:M35")
riga = 8
If Target.Count = 1 Then
tabella.ClearContents
If Not Intersect(Target, Me.Range("B1")) Is Nothing And Target.Value > 0 Then
Application.EnableEvents = False
For Each cl In area
If cl.Value = "" Then Exit For
If Val(Mid(cl.Value, Len(cl.Value) - 3, Len(cl.Value))) = anno Then
Me.Range("A" & riga).Value = cl.Offset(0, 1).Value
Me.Cells(riga, Month(CDate(cl)) + 1).Value = cl.Offset(0, 3).Value
riga = riga + 1
tot = tot + 1
End If
Next cl
If tot > 0 Then
MsgBox "Trovato " & tot & " valori per l'anno " & anno, vbInformation
Else
MsgBox "Nessun valore trovato per l'anno " & anno, vbExclamation
End If
Application.EnableEvents = True
End If
End If
Set area = Nothing
Set ws2 = Nothing
Set tabella = Nothing
End Sub
|