Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao Giuseppe,
Purtroppo non sono (completamente) omnisciente! :-) Quindi, non avevo previsto la possibilità che tu avresti voluto cancallare tutti i dati nei tutti i 31 fogli. Pertanto, quando tu cancella i dati nel trentunnesimo foglio, avendo prima cancellato tutti dati negli altri 30 fogli interessati, riscontri l'errore riportata da te. Per la tua informazione, l'errore viene riscontrato perchè la routine SortedList non può ordinare un elenco vuoto.
Per affrrontare e superare questa possibiltà precedentemente imprevista da me, sostituisci tutto il codice nel ruo modulo standard con la seguente versione nella quale le modifiche sono evidenziate in grassetto:
'=========>>
Option Explicit
Public Const sFoglioRepielogoOre As String = "Rpg ORE"
Public Const sFoglioRepielogoCommesse As String = "Rpg CMS"
Public Const iPrimaRigaDati As Long = 12
'--------->>
Public Sub Tester(aSH As Worksheet, iCol As Long, destSH As Worksheet)
Dim WB As Workbook
Dim oSH As Worksheet
Dim srcRng As Range, destRng As Range, rCell As Range
Dim arrIn As Variant, arrKeys As Variant
Dim oDic As Object
Dim aStr As String
Dim i As Long
Dim iRow As Long, jRow As Long, LRow As Long
Dim CalcMode As Long
'
Set WB = ThisWorkbook
Set oDic = CreateObject("Scripting.Dictionary")
oDic.CompareMode = 1 '\ TextCompare
For Each oSH In WB.Worksheets
If oSH.Name Like "##" Then
Debug.Print oSH.Name
Set srcRng = Nothing
With oSH
iRow = LastRow(oSH, .Columns(iCol))
If iRow >= iPrimaRigaDati Then
Set srcRng = .Cells(iPrimaRigaDati, iCol). _
Resize(iRow - iPrimaRigaDati + 1)
End If
End With
If Not srcRng Is Nothing Then
arrIn = srcRng.Value
If IsArray(arrIn) Then
For i = LBound(arrIn) To UBound(arrIn)
aStr = arrIn(i, 1)
With oDic
If Not .exists(aStr) Then
.Add Key:=aStr, Item:=vbNullString
End If
End With
Next i
Else
aStr = arrIn
With oDic
If Not .exists(aStr) Then
.Add Key:=aStr, Item:=vbNullString
End If
End With
End If
End If
End If
Next oSH
With destSH
jRow = LastRow(destSH, .Columns("A:A"))
If jRow > 2 Then
Set destRng = .Range("A3:A" & jRow)
Else
Set destRng = .Range("A3")
End If
End With
On Error GoTo XIT
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
If oDic.Count = 0 Then
destRng.ClearContents
GoTo XIT
End If
arrKeys = SortedList(oDic.keys)
With destRng
.ClearContents
.Resize(UBound(arrKeys)).Value = _
Application.Transpose(arrKeys)
End With
XIT:
Set oDic = Nothing
With Application
.Calculation = CalcMode
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
'--------->>
Public Function SortedList(V As Variant)
Dim oSortedList As Object
Dim arrOut() As Variant
Dim sStr As String
Dim i As Long
Set oSortedList = CreateObject("System.Collections.Sortedlist")
With oSortedList
For i = LBound(V) To UBound(V)
sStr = V(i) ', 1)
If Not sStr = vbNullString Then
If Not .ContainsKey(sStr) Then
.Add Key:=sStr, Value:=i
End If
End If
Next i
ReDim arrOut(1 To .Count)
For i = 0 To .Count - 1
arrOut(i + 1) = .GetKey(i)
Next i
End With
SortedList = arrOut
End Function
'--------->>
Public Function LastRow(SH As Worksheet, _
Optional Rng As Range, _
Optional minRow As Long = 1)
If Rng Is Nothing Then
Set Rng = SH.Cells
End If
On Error Resume Next
LastRow = Rng.Find(What:="*", _
after:=Rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
If LastRow < minRow Then
LastRow = minRow
End If
End Function
'<<=========
===
Regards,
Norman