Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Uhm... Prova così:
' Standard Module : Modulo1
'
Option Explicit
Public Sub ListaNuoviClientiAnnoMese()
On Error GoTo ErrH
Const cstrWsh As String = "Foglio1"
Const cstrRng As String = "A1"
Const clngCliOffset As Long = 3
Const cstrWshNew As String = "Risultato"
Dim wsh As Excel.Worksheet
Dim rng As Excel.Range
Dim colCli As VBA.Collection
Dim r As Long
Dim e As Long
Dim strKey As String
Dim strItem As String
Dim i As Long
Dim colDate As VBA.Collection
Dim lngCount As Long
Set wsh = ThisWorkbook.Worksheets(cstrWsh)
With wsh
Set rng = .Range(.Range(cstrRng), _
.Cells(.Rows.Count, _
.Range(cstrRng).Column _
).End(xlUp))
End With
If rng.Rows.Count = 1 Then
MsgBox "Nessun dato."
GoTo ExtP
End If
Set colCli = New VBA.Collection
For r = 1 To rng.Count - 1
With rng
strKey = "K" & .Offset(r).Resize(1).Value
strItem = Format$(.Offset(r, clngCliOffset).Resize(1).Value, _
"yyyy-mm")
End With
On Error Resume Next
With colCli
.Add strItem, strKey
e = Err.Number
On Error GoTo ErrH
If e Then
If .Item(strKey) > strItem Then
.Remove strKey
.Add strItem, strKey
End If
End If
End With
Next
Set colDate = New VBA.Collection
For i = 1 To colCli.Count
With colCli
strKey = "K" & .Item(i)
strItem = .Item(i)
End With
With colDate
On Error Resume Next
.Add Array(strItem, 1), strKey
e = Err.Number
On Error GoTo ErrH
If e Then
strItem = .Item(strKey)(0)
lngCount = .Item(strKey)(1) + 1
.Remove strKey
.Add Array(strItem, lngCount), strKey
End If
End With
Next
Application.DisplayAlerts = False
With ThisWorkbook.Worksheets
On Error Resume Next
.Item(cstrWshNew).Delete
On Error GoTo ErrH
With .Add
.Name = cstrWshNew
.Range("A2").Value = "NUOVI CLIENTI"
For i = 1 To colDate.Count
With .Range("A1")
With .Offset(0, i)
.Value = DateValue(colDate(i)(0) & "-01")
.NumberFormat = "yyyy-mm"
End With
.Offset(1, i).Value = colDate(i)(1)
End With
Next
With .Sort
With .SortFields
.Clear
.Add Key:=Range("B1").Resize(1, colDate.Count), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
End With
.SetRange Range("B1").Resize(2, colDate.Count)
.Header = xlNo
.Orientation = xlLeftToRight
.Apply
End With
.Columns.AutoFit
.Range("A1").Select
End With
End With
ExtP: On Error Resume Next
Application.DisplayAlerts = True
Set colDate = Nothing
Set colCli = Nothing
Set rng = Nothing
Set wsh = Nothing
On Error GoTo 0
Exit Sub
ErrH: MsgBox Err.Description
Resume ExtP
End Sub