
DATA Lotto mc caricati mc scaricati
09/03/2015 Lotto 1 2,5
18/03/2015 Lotto 1-c 2,5
10/04/2015 Lotto 2 0,5
14/05/2015 Lotto 2-c 0,5
03/06/2015 Lotto 3 4
11/06/2015 Lotto 4 4
18/06/2015 Lotto 3/4-c 8
12/06/2015 Lotto 5 8
19/06/2015 Lotto 5-c 8
16/07/2015 Lotto 6 2
Option Explicit
Sub calc_diffdays()
Dim rg As Range, cell As Range, c As Range
Dim num_lotto() As Integer, tmp() As Integer, i As Long, s As String
Dim k As Integer, m As String, scaricato_multiplo As Boolean, z As Integer
Dim date1 As Date, date2 As Date
Set rg = Range("b2:b" & Range("b2").End(xlDown).Row)
Range("f2:f" & Range("b2").End(xlDown).Row).ClearContents
For i = 1 To rg.Rows.Count
s = rg(i)
num_lotto() = strip_numbers(s)
If num_lotto(0) = 1 Then
'lotti singoli
If InStr(s, "-") = 0 Then
'lotti singoli "caricati"
Cells(i + 1, "F") = "caricato " & num_lotto(1)
date1 = Cells(i + 1, "A")
Set c = rg.Find(s & "-c", lookat:=xlWhole)
If c Is Nothing Then
'lotti non scaricati
scaricato_multiplo = False
For k = i + 1 To rg.Rows.Count
tmp() = strip_numbers(rg(k))
If tmp(0) > 1 Then
For z = 1 To tmp(0)
If num_lotto(1) = tmp(z) Then scaricato_multiplo = True
Next
End If
Next
If scaricato_multiplo Then
Cells(i + 1, "F") = Cells(i + 1, "F") & " (scaricato successivamente)"
Else
Cells(i + 1, "F") = "caricato ma non scaricato " & num_lotto(1)
End If
Else
'lotti singoli "scaricati"
Cells(c.Row, "F") = "scaricato " & num_lotto(1)
date2 = Cells(c.Row, "A")
Cells(c.Row, "E") = DateDiff("d", date1, date2)
End If
End If
Else
'lotti multipli
m = ""
For k = 1 To num_lotto(0)
m = m & num_lotto(k) & ", "
Next
Cells(i + 1, "F") = "scaricati " & Left(m, Len(m) - 2)
End If
Next
End Sub
Private Function strip_numbers(s As String) As Variant
Dim i As Integer, m As String, j As Integer, arr() As Integer
j = 0
For i = 1 To Len(s)
m = Mid(s, i, 1)
If m Like "[0-9]" Then
j = j + 1
ReDim Preserve arr(j) As Integer
arr(j) = Val(m)
End If
Next
arr(0) = j
strip_numbers = arr()
End Function
|
Option Explicit
Sub calc_diffdays()
Dim rg As Range, cell As Range, c As Range
Dim num_lotto() As Integer, tmp As Variant, i As Long, s As String
Dim k As Integer, m As String, scaricato_multiplo As Boolean
Dim date1 As Date, date2 As Date, f As String
Dim re As Object, strip_numbers As Variant, v As Variant
Dim carico As Collection, scarico As Collection
Dim initial_row As Long, end_row As Long
initial_row = Range("C2:C1000").Find("Lotto", lookat:=xlWhole).Row + 1
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "(d+)"
re.IgnoreCase = True
re.Global = True
Set rg = Range(Cells(initial_row, "C"), Cells(Cells(initial_row, "C").End(xlDown).Row, "C"))
end_row = initial_row + rg.Rows.Count - 1
Range("A13:A100").ClearContents
Range("L13:L100").ClearContents '<<< debug only
Set carico = New Collection
Set scarico = New Collection
For i = initial_row To end_row
s = rg.Cells(i - initial_row + 1)
Set strip_numbers = re.Execute(s)
If strip_numbers.Count = 1 Then
'lotti singoli
If InStr(s, "-") = 0 Then
'lotti singoli "caricati"
Cells(i, "L") = "caricato " & strip_numbers(0) '<<< debug only
date1 = Cells(i, "B") 'data di carico del lotto
Set c = rg.Find(s & "-c", lookat:=xlWhole)
If c Is Nothing Then
'lotti non scaricati
scaricato_multiplo = False
For k = i + 1 To end_row
Set tmp = re.Execute(rg.Cells(k - initial_row + 1))
If tmp.Count > 1 Then
For Each v In tmp
If strip_numbers(0) = Trim(v) Then scaricato_multiplo = True
Next
End If
Next
If scaricato_multiplo Then
Cells(i, "L") = Cells(i, "L") & " (scaricato successivamente)" '<<< debug only
carico.Add Cells(i, "B"), (strip_numbers(0))
Else
Cells(i, "L") = "caricato ma non scaricato " & strip_numbers(0) '<<< debug only
End If
Else
'lotti singoli "scaricati"
Cells(c.Row, "L") = "scaricato " & strip_numbers(0) '<<< debug only
date2 = Cells(c.Row, "B")
Cells(c.Row, "A") = DateDiff("d", date1, date2)
End If
End If
Else
'lotti multipli
m = "": f = ""
For Each v In strip_numbers
m = m & Trim(v) & ", "
date1 = carico((v))
date2 = Cells(i + 1, "B")
f = f & Trim(v) & ": " & DateDiff("d", date1, date2) & " giorni, "
Next
Cells(i, "A") = Left(f, Len(f) - 2)
Cells(i, "L") = "scaricati " & Left(m, Len(m) - 2) '<<< debug only
End If
Next
End Sub
|
Option Explicit
Sub calc_diffdays()
Dim rg As Range, c As Range
Dim i As Long, s As String
Dim date1 As Date, date2 As Date
Dim re As Object, strip_numbers As Variant
Dim j As Long
Dim initial_row As Long, final_row As Long
initial_row = Range("C2:C1000").Find("Lotto", lookat:=xlWhole).Row + 1
Set rg = Range(Cells(initial_row, "C"), Cells(Cells(initial_row, "C").End(xlDown).Row, "C"))
final_row = initial_row + rg.Rows.Count - 1
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "(d+)"
re.IgnoreCase = True
re.Global = True
Range("A13:A100").ClearContents
j = 0
For i = initial_row To final_row
s = rg.Cells(i - initial_row + 1)
j = j + 1
If InStr(s, "c") > 0 Then
date2 = rg.Cells(i - initial_row + 1).Offset(, -1)
Set strip_numbers = re.Execute(s)
Set c = Range(Cells(initial_row, "C"), Cells(final_row, "C")).Find("Lotto " & strip_numbers(0), LookIn:=xlValues)
date1 = c.Offset(, -1)
Cells(i, "A") = DateDiff("d", date1, date2)
j = 0
End If
Next
If j <> 0 Then
'un carico è rimasto in sospeso, l'ultimo
Cells(i, "A") = DateDiff("d", Cells(i - 1, "B"), Now)
End If
End Sub
|
Option Explicit
Sub calc_diffdays()
Dim rg As Range, c As Range
Dim i As Long, s As String
Dim date1 As Date, date2 As Date
Dim re As Object, strip_numbers As Variant
Dim j As Long
Dim initial_row As Long, final_row As Long
Application.EnableEvents = False
initial_row = Range("C2:C1000").Find("Lotto", lookat:=xlWhole).Row + 1
Set rg = Range(Cells(initial_row, "C"), Cells(Cells(initial_row, "C").End(xlDown).Row, "C"))
If rg.Cells.Count > 1048500 Then
final_row = 13
Else
final_row = initial_row + rg.Rows.Count - 1
End If
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "(d+)"
re.IgnoreCase = True
re.Global = True
With Range("A13:A100")
.Font.Bold = True
.ClearContents
End With
j = 0
For i = initial_row To final_row
s = rg.Cells(i - initial_row + 1)
j = j + 1
If InStr(s, "c") > 0 Then
date2 = rg.Cells(i - initial_row + 1).Offset(, -1)
Set strip_numbers = re.Execute(s)
Set c = Range(Cells(initial_row, "C"), Cells(final_row, "C")).Find("Lotto " & strip_numbers(0), LookIn:=xlValues)
date1 = c.Offset(, -1)
Cells(i, "A") = DateDiff("d", date1, date2)
j = 0
End If
Next
If j <> 0 Then
'un carico è rimasto in sospeso, l'ultimo
Cells(i, "A") = DateDiff("d", Cells(i - j, "B"), Now)
End If
Application.EnableEvents = True
End Sub
|
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim re As Object, strip_numbers As Variant, s As String
Dim last As Variant, i As Long
If Intersect(Target, Range("D:D, F:F")) Is Nothing Then Exit Sub
If WorksheetFunction.CountA(Target) = 0 Then Exit Sub 'premuto Canc sulla selezione
If Target.Row < 13 Then Exit Sub
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "(d+)"
re.IgnoreCase = True
re.Global = True
On Error GoTo exit_here
Select Case Target.Column
Case 4 'colonna D, carico
If Target.Offset(, -1) <> "" Then Exit Sub
If Target.Row = 13 Then
last = 0
Else
s = Target.Offset(-1, -1)
Set strip_numbers = re.Execute(s)
last = strip_numbers(strip_numbers.Count - 1)
End If
Application.EnableEvents = False
'crea nuovo lotto in carico
Target.Offset(, -1) = "Lotto " & last + 1
'sistema la data attuale nella colonna A
Target.Offset(, -2) = Now
Case 6 'colonna F, smaltimento
If Target = "" Then
With Range(Cells(Target.Row, "B"), Cells(Target.Row, "K")).Borders(xlEdgeBottom)
.LineStyle = xlDash
.Weight = xlHairline
End With
End If
If Target.Offset(, -3) <> "" Then Exit Sub
Application.EnableEvents = False
s = ""
For i = Target.Row - 1 To 13 Step -1
If InStr(Cells(i, "C"), "c") > 0 Then
If s <> "" Then
s = Left(s, Len(s) - 1) & "-c"
Cells(Target.Row, "C") = "Lotto " & s
Cells(Target.Row, "B") = Now
Range(Cells(Target.Row, "B"), Cells(Target.Row, "K")).Borders(xlEdgeBottom).Weight = xlThin
Exit For
End If
Else
s = re.Execute(Cells(i, "C"))(0) & "/" & s
End If
Next
End Select
If s <> "" Then
If Right(s, 1) = "/" Then
s = Left(s, Len(s) - 1) & "-c"
Cells(Target.Row, "B") = Now
Cells(Target.Row, "C") = "Lotto " & s
End If
End If
Call calc_diffdays
exit_here:
Application.EnableEvents = True
End Sub
|
