
Option Explicit
Sub find_triplets()
Dim my_range As Range, single_row As Range, single_cell As Range
Dim triplets(10) As String, i As Integer, c As Range
Dim found_at As String, relative_row As Integer, j As Integer, s As String
Dim v As Variant, s1 As String, s2 As String, next_row As Range, t As Integer
Set my_range = Range("A2:F6")
Range("H:H").ClearContents
For Each single_row In my_range.Rows.Resize(my_range.Rows.Count - 1) 'esclude l'ultima riga del range iniziale
triplets(1) = Format(single_row.Cells(2), "00") & ";" & Format(single_row.Cells(3), "00") & ";" & Format(single_row.Cells(4), "00")
triplets(2) = Format(single_row.Cells(2), "00") & ";" & Format(single_row.Cells(3), "00") & ";" & Format(single_row.Cells(5), "00")
triplets(3) = Format(single_row.Cells(2), "00") & ";" & Format(single_row.Cells(3), "00") & ";" & Format(single_row.Cells(6), "00")
triplets(4) = Format(single_row.Cells(2), "00") & ";" & Format(single_row.Cells(4), "00") & ";" & Format(single_row.Cells(5), "00")
triplets(5) = Format(single_row.Cells(2), "00") & ";" & Format(single_row.Cells(4), "00") & ";" & Format(single_row.Cells(6), "00")
triplets(6) = Format(single_row.Cells(2), "00") & ";" & Format(single_row.Cells(5), "00") & ";" & Format(single_row.Cells(6), "00")
triplets(7) = Format(single_row.Cells(3), "00") & ";" & Format(single_row.Cells(4), "00") & ";" & Format(single_row.Cells(5), "00")
triplets(8) = Format(single_row.Cells(3), "00") & ";" & Format(single_row.Cells(4), "00") & ";" & Format(single_row.Cells(6), "00")
triplets(9) = Format(single_row.Cells(3), "00") & ";" & Format(single_row.Cells(5), "00") & ";" & Format(single_row.Cells(6), "00")
triplets(10) = Format(single_row.Cells(4), "00") & ";" & Format(single_row.Cells(5), "00") & ";" & Format(single_row.Cells(6), "00")
i = single_row.Row - 1
For Each next_row In my_range.Offset(i, 1).Resize(my_range.Rows.Count - i, my_range.Columns.Count - 1).Rows
For t = 1 To 10
v = Split(triplets(t), ";")
'v = bubble_sort(v) '<<< togliere il commento per avere l'elenco ordinato da piccolo a grande
s1 = Join(v, ";")
s2 = flatten(next_row)
v = Split(s2, ";")
'v = bubble_sort(v) '<<< togliere il commento per avere l'elenco ordinato da piccolo a grande
s2 = Join(v, ";")
v = Split(s1, ";")
If InStr(s2, Format(v(0), "00")) > 0 And InStr(s2, Format(v(1), "00")) > 0 And InStr(s2, Format(v(2), "00")) > 0 Then
j = j + 1
Cells(j, "H") = "riga " & i & " - " & next_row.Row - 1 & ": " & s1
End If
Next t
Next next_row
Next single_row
End Sub
Private Function flatten(r As Range) As String
Dim vector(1 To 5) As String, i As Integer
For i = 1 To 5
vector(i) = r.Cells(i)
Next
flatten = Join(vector, ";")
End Function
Private Function bubble_sort(vector As Variant) As Variant
Dim cnt1 As Long, cnt2 As Long, tmp As Long
For cnt1 = UBound(vector) To LBound(vector) Step -1
For cnt2 = LBound(vector) + 1 To cnt1
If vector(cnt2 - 1) > vector(cnt2) Then
tmp = vector(cnt2 - 1)
vector(cnt2 - 1) = vector(cnt2)
vector(cnt2) = tmp
End If
Next
Next
bubble_sort = vector
End Function
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Sub CheckTripleRE()
'by scossa
Dim rng As Range
Dim rRow As Range
Dim rRowUT As Range
Dim aRowUT As Variant
Dim aStrRowUT As Variant
Dim j As Long
Dim k As Long
Dim nRows As Long
Dim i As Long
Dim oRE As Object 'RegExp
Dim oMatch As Object 'MatchCollection
Dim sPatt As String
Dim sPattPre As String
Dim vItem As Variant
Dim sTrip As String
Application.ScreenUpdating = False
Set oRE = CreateObject("vbscript.regexp") 'New RegExp '
Set rng = Foglio1.Range("B2:F6")
nRows = rng.Rows.Count
ReDim aStrRowUT(2 To nRows)
With oRE
.Global = True
.IgnoreCase = True
End With
Foglio1.Range("H2:H1000").ClearContents
i = 1
For k = 1 To nRows - 1
sPatt = Join(Application.Transpose(Application.Transpose(rng.Rows(k).Cells)), "|")
oRE.Pattern = sPatt
For j = k + 1 To nRows
Set rRowUT = rng.Rows(j).Cells
aRowUT = Application.Transpose(Application.Transpose(rRowUT.Value))
aStrRowUT(j) = "#" & Join(aRowUT, "#") & "#"
If oRE.Test(aStrRowUT(j)) Then
Set oMatch = oRE.Execute(aStrRowUT(j))
If oMatch.Count > 2 Then
For Each vItem In oMatch
sTrip = sTrip & vItem & "; "
Next
Debug.Print "tripletta " & sTrip & " presente in riga " & j
If sPattPre <> sPatt Then
i = i + 1
Foglio1.Cells(i, 8).Value = "etrazione " & k & " (" & Replace(sPatt, "|", "; ") & ")"
sPattPre = sPatt
i = i + 1
End If
Foglio1.Cells(i, 8).Value = "tripletta " & sTrip & " presente in riga " & j
i = i + 1
End If
End If
sTrip = ""
Next j
Next k
Application.ScreenUpdating = True
Set oRE = Nothing
Set oMatch = Nothing
Set rRow = Nothing
Set rRowUT = Nothing
Set rng = Nothing
End Sub
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
'siamo nel ciclo for each next_row...: tra s2 = flatten(next_row) e v = Split(s1, ";") va messo il codice seguente che sostituisce il precedente:
...
v = Split(s2, ";")
For z = 0 To UBound(v)
v(z) = Format(v(z), "00")
Next
v = bubble_sort(v) '<<< togliere il commento per avere l'elenco ordinato da piccolo a grande
s2 = Join(v, ";")
... |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Sub CheckTripleRE()
'by scossa
Dim rng As Range
Dim rRow As Range
Dim rRowUT As Range
Dim aRowUT As Variant
Dim aStrRowUT As Variant
Dim j As Long
Dim k As Long
Dim nRows As Long
Dim i As Long
Dim oRE As Object 'RegExp
Dim oMatch As Object 'MatchCollection
Dim sPatt As String
Dim sPattPre As String
Dim vItem As Variant
Dim sTrip As String
Application.ScreenUpdating = False
Set oRE = CreateObject("vbscript.regexp") 'New RegExp '
Set rng = Foglio1.Range("B2:F6")
nRows = rng.Rows.Count
ReDim aStrRowUT(2 To nRows)
With oRE
.Global = True
.IgnoreCase = True
End With
Foglio1.Range("H2:H1000").ClearContents
i = 1
For k = 1 To nRows - 1
sPatt = "#" & Join(Application.Transpose(Application.Transpose(rng.Rows(k).Cells)), "#|#") & "#"
oRE.Pattern = sPatt
For j = k + 1 To nRows
Set rRowUT = rng.Rows(j).Cells
aRowUT = Application.Transpose(Application.Transpose(rRowUT.Value))
aStrRowUT(j) = "#" & Join(aRowUT, "##") & "#"
If oRE.Test(aStrRowUT(j)) Then
Set oMatch = oRE.Execute(aStrRowUT(j))
If oMatch.Count > 2 Then
For Each vItem In oMatch
sTrip = sTrip & Replace(vItem, "#", "") & "; "
Next
If sPattPre <> sPatt Then
i = i + 1
Foglio1.Cells(i, 8).Value = "etrazione " & k & " (" & Replace(Replace(sPatt, "#", ""), "|", "; ") & ")"
sPattPre = sPatt
i = i + 1
End If
Foglio1.Cells(i, 8).Value = "tripletta " & sTrip & " presente in riga " & j
i = i + 1
End If
End If
sTrip = ""
Next j
Next k
Application.ScreenUpdating = True
Set oRE = Nothing
Set oMatch = Nothing
Set rRow = Nothing
Set rRowUT = Nothing
Set rng = Nothing
End Sub
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Foglio1.Cells(i, 8).Value = "tripletta " & sTrip Foglio1.Cells(i, 9).Value = j |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Sub CheckTripleRE_v1b()
'by scossa
Dim rng As Range
Dim rRow As Range
Dim rRowUT As Range
Dim aRowUT As Variant
Dim aStrRowUT As Variant
Dim j As Long
Dim k As Long
Dim nRows As Long
Dim i As Long
Dim oRE As Object 'RegExp
Dim oMatch As Object 'MatchCollection
Dim sPatt As String
Dim sPattPre As String
Dim vItem As Variant
Dim sTrip As String
Application.ScreenUpdating = False
Set oRE = CreateObject("vbscript.regexp") 'New RegExp '
Set rng = Foglio1.Range("B2:F6")
nRows = rng.Rows.Count
ReDim aStrRowUT(2 To nRows)
With oRE
.Global = True
.IgnoreCase = True
End With
Foglio1.Range("H2:K1000").ClearContents
i = 1
For k = 1 To nRows - 1
sPatt = "#" & Join(Application.Transpose(Application.Transpose(rng.Rows(k).Cells)), "#|#") & "#"
oRE.Pattern = sPatt
For j = k + 1 To nRows
Set rRowUT = rng.Rows(j).Cells
aRowUT = Application.Transpose(Application.Transpose(rRowUT.Value))
aStrRowUT(j) = "#" & Join(aRowUT, "##") & "#"
If oRE.Test(aStrRowUT(j)) Then
Set oMatch = oRE.Execute(aStrRowUT(j))
If oMatch.Count = 3 Then
For Each vItem In oMatch
sTrip = sTrip & Replace(vItem, "#", "") & "; "
Next
If sPattPre <> sPatt Then
i = i + 1
Foglio1.Cells(i, 8).Value = "etrazione " & k & " (" & Replace(Replace(sPatt, "#", ""), "|", "; ") & ")"
sPattPre = sPatt
i = i + 1
End If
Foglio1.Cells(i, 8).Value = "tripletta " & sTrip & " presente in riga " & j
Foglio1.Cells(i, 9).Value = Split(sTrip, "; ")(0)
Foglio1.Cells(i, 10).Value = Split(sTrip, "; ")(1)
Foglio1.Cells(i, 11).Value = Split(sTrip, "; ")(2)
i = i + 1
End If
End If
sTrip = ""
Next j
Next k
Application.ScreenUpdating = True
Set oRE = Nothing
Set oMatch = Nothing
Set rRow = Nothing
Set rRowUT = Nothing
Set rng = Nothing
End Sub
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
