
Option Explicit
Sub Aggiorna()
Dim matr1(), matr2(), ns
Dim rg As Integer, a As Integer, pr As Integer, dp As Integer
Dim i As Integer, prec As String, nuovodato As String
rg = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row + 1 'assume ultima riga
'trasferisce tutti i dati presenti a matr1
ReDim matr1(1 To rg), matr2(1 To rg)
For i = 4 To rg - 1
ns = Split(Cells(i, 1), "-")
If UBound(ns) > 0 Then
pr = ns(0)
dp = ns(1)
Else
pr = ns(0)
dp = "0"
End If
matr1(i) = pr: matr2(i) = dp
Next i
'rinumerazione
a = 1
For i = 4 To rg - 1
If matr2(i) = "0" Or _
(matr2(i) <> "0" And matr1(i) <> matr1(i - 1)) Then a = 1
If matr2(i) <> "0" Then
matr2(i) = Trim(Str(a)): a = a + 1
End If
Next i
For i = 4 To rg - 1
'primo esame
If i = 4 And matr1(i) = "1" Then
nuovodato = matr1(i) & "-" & matr2(i): GoTo 1 'uguale a 1,salta al successivo
ElseIf i = 4 And Val(matr1(i)) > 1 Then 'maggiore di 1
matr1(i) = "1": nuovodato = matr1(i) & "-" & matr2(i)
GoTo 1 'rinumera partendo da 1
End If
'secondo esame
If matr1(i) = Val(matr1(i - 1)) + 1 Then
prec = matr1(i)
matr1(i) = Trim(Str(Val(matr1(i - 1)) + 1)) 'rinumera prec+1
nuovodato = matr1(i) & "-" & matr2(i)
'terzo esame
ElseIf matr1(i) > Val(matr1(i - 1)) + 1 Then 'maggiore precedente più di 1 unità
If prec <> matr1(i) Then
prec = matr1(i)
matr1(i) = Trim(Str(Val(matr1(i - 1)) + 1)) 'rinumera prec+1
nuovodato = matr1(i) & "-" & matr2(i)
ElseIf prec = matr1(i) Then
prec = matr1(i)
matr1(i) = matr1(i - 1) 'assegna rinum-prec
nuovodato = matr1(i) & "-" & matr2(i) 'assegna suffisso matr2
ElseIf Val(matr1(i)) < Val(matr1(i + 1)) Then 'minore del successivo
prec = matr1(i)
matr1(i) = Trim(Str(Val(matr1(i - 1)) + 1)) 'rinumera prec+1
nuovodato = matr1(i) & "-" & matr2(i)
ElseIf Val(matr1(i)) = Val(matr1(i + 1)) Then 'uguale al successivo
prec = matr1(i)
matr1(i) = matr1(i - 1) 'assegna rinum-prec
nuovodato = matr1(i) & "-" & matr2(i) 'assegna suffisso matr2
End If
End If
1 If matr2(i) = "0" Then nuovodato = matr1(i)
Cells(i, 4) = nuovodato
Next i
End Sub
|
Option Explicit
Sub aggiorna_byVF()
Dim cell As Range, v As Variant
Dim genitore As Integer
Dim padre As Collection, figlio As Integer
Set padre = New Collection
On Error GoTo gest_err
For Each cell In Range("A4:A" & Range("A" & Rows.Count).End(xlUp).Row)
genitore = genitore + 1
If InStr(cell, "-") > 0 Then
v = Split(cell, "-")
padre.Add Array(genitore, 0), v(0)
Cells(cell.Row, "D") = Join(padre(v(0)), "-")
Else
padre.Add Array(genitore, 0), cell
Cells(cell.Row, "D") = genitore
End If
Next
Exit Sub
gest_err:
If Err.Number = 457 Then
genitore = padre(v(0))(0)
figlio = padre(v(0))(1) + 1
padre.Remove v(0)
padre.Add Array(genitore, figlio), v(0)
Resume Next
Else
Err.Raise Err.Number
Exit Sub
End If
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) |
On Error GoTo gest_err
v = 2 / 0 |
| 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) |
Else
'Err.Raise Err.Number
'Exit Sub
Stop
End If
|
