
Option Explicit
Sub calcola()
Dim X As Long, Y As Long, Uriga As Long
Uriga = Range("A" & Rows.Count).End(xlUp).Row
ActiveWorkbook.Worksheets("Foglio1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Foglio1").Sort.SortFields.Add Key:=Range("A2:A" & Uriga _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Foglio1").Sort
.SetRange Range("A1:B" & Uriga)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For X = 2 To Uriga
For Y = X + 1 To Uriga
If Cells(X, 1) = Cells(Y, 1) Then
If Cells(X, 2) > (Cells(Y, 2) - 30) Then
Cells(X, 3) = 1
End If
Else
Exit For
End If
Next Y
Next X
Cells(1, 3) = "Totale = " & Application.WorksheetFunction.CountIf(Range("C2:C" & Uriga), 1)
MsgBox ("fatto")
End Sub |
Option Explicit
Sub calcola()
Dim X As Long, Y As Long, R As Long, Uriga As Long, DD As Date, Codice As String
If Cells(2, 5) <> "" Then Exit Sub 'finche non metti il riordino dei record
Dim sh1 As Worksheet: Set sh1 = Worksheets("Foglio1") ' da cambiare casomai
Uriga = sh1.Range("A" & Rows.Count).End(xlUp).Row
sh1.Range("C1:E" & Uriga).ClearContents
sh1.Columns("C:E").NumberFormat = "General"
sh1.Cells(1, 4) = "Ripetuti per cliente"
sh1.Cells(1, 5) = "Progressivo"
For X = 2 To Uriga
DD = Left(sh1.Cells(X, 2), 10)
sh1.Cells(X, 2) = DD
sh1.Cells(X, 5) = X
Next X
sh1.Sort.SortFields.Clear
sh1.Sort.SortFields.Add Key:=Range("A2:A" & Uriga), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
sh1.Sort.SortFields.Add Key:=Range("B2:B" & Uriga), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With sh1.Sort
.SetRange Range("A1:E" & Uriga)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For X = 2 To Uriga
R = 0
Codice = sh1.Cells(X, 1)
For Y = X + 1 To Uriga
If sh1.Cells(Y, 1) = Codice Then
If sh1.Cells(X, 2) > sh1.Cells(Y, 2) - 30 Then
sh1.Cells(X, 3) = 1
R = R + 1
End If
Else
If sh1.Cells(Y, 1) <> Codice Then If R <> 0 Then sh1.Cells(X, 4) = sh1.Cells(X, 4) + R
Exit For
End If
Next Y
Next X
sh1.Cells(1, 3) = "Totale = " & Application.WorksheetFunction.CountIf(Range("C2:C" & Uriga), 1)
'--------------------Riordino, eliminare la4 riga in alto
'sh1.Sort.SortFields.Clear
'sh1.Sort.SortFields.Add Key:=Range("E1:E" & Uriga), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
'With sh1.Sort
'.SetRange Range("A1:E" & Uriga)
'.Header = xlYes
'.MatchCase = False
'.Orientation = xlTopToBottom
'.SortMethod = xlPinYin
'.Apply
'End With
MsgBox ("fatto")
Set sh1 = Nothing
End Sub |
Option Explicit
Sub calcola()
Dim X As Long, Y As Long, R As Long, Uriga As Long, DD As Date, Codice As String
Dim sh1 As Worksheet: Set sh1 = Worksheets("Foglio1") ' da cambiare casomai
Uriga = sh1.Range("A" & Rows.Count).End(xlUp).Row
sh1.Range("C1:C" & Uriga).ClearContents
sh1.Columns("C:C").NumberFormat = "General"
For X = 2 To Uriga
DD = Left(sh1.Cells(X, 2), 10)
sh1.Cells(X, 2) = DD
Next X
sh1.Sort.SortFields.Clear
sh1.Sort.SortFields.Add Key:=Range("A2:A" & Uriga), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
sh1.Sort.SortFields.Add Key:=Range("B2:B" & Uriga), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With sh1.Sort
.SetRange Range("A1:C" & Uriga)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For X = 2 To Uriga
Codice = sh1.Cells(X, 1)
For Y = X + 1 To Uriga
If sh1.Cells(Y, 1) = Codice Then
If sh1.Cells(X, 2) > sh1.Cells(Y, 2) - 30 Then
sh1.Cells(X, 3) = 1
End If
Else
Exit For
End If
Next Y
Next X
sh1.Cells(1, 3) = "Totale = " & Application.WorksheetFunction.CountIf(Range("C2:C" & Uriga), 1)
MsgBox ("fatto")
Set sh1 = Nothing
End Sub |
