Sub Ordinamento()
With Application
.EnableEvents = False
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
Sheets.Item(1).Activate
Sheets.Item(1).Select
Sheets.Item(1).Columns.NumberFormat = "@"
Dim i, g, tipo5, tipo6, tipo7, tipo8, tipo9 As Integer
i = 1
g = 1
Do While Not IsEmpty(Sheets(1).Cells(i, 1)) 'ciclo per far scorrere tutte le righe di input
If Mid(Sheets(1).Cells(i, 1), 14, 1) = "1" Then 'controllo per identificare la presenza del tipo informazione 1 che costituisce l'inizione del blocco informativo
tipo5 = 0 'azzero i contatori dei tipo informazione
tipo6 = 0
tipo7 = 0
tipo8 = 0
tipo9 = 0
i = i + 1
Do While Mid(Sheets(1).Cells(i, 1), 15, 20) = Mid(Sheets(1).Cells(i - 1, 1), 15, 20) 'tutti i record dati appartenti al medesimo blocco informativo hanno gli stessi caratteri nella parte iniziale della stringa (dalla posizione 15 per 20 caratteri). In questo modo ho costruito un ciclo che lavora ogni record del blocco e basta
Select Case Mid(Sheets(1).Cells(i, 1), 14, 1) 'faccio contare i tipo informazione presenti
Case "5"
tipo5 = tipo5 + 1
Case "6"
tipo6 = tipo6 + 1
Case "7"
tipo7 = tipo7 + 1
Case "8"
tipo8 = tipo8 + 1
Case "9"
tipo9 = tipo9 + 1
Case Else
messaggio = MsgBox("tipo informazione sconosciuto", 0, "Record con tipo info errato - record " & i) 'versioni precedenti del data base avevano altre codifiche dei tipo informazione, questo mi dovrebbe consentire di rilevare se erroneamente sto lavorando un database vecchio.
End Select
i = i + 1
Loop
Else
messaggio = MsgBox("La registrazione non inizia con un tipo informazione 1", 0, "Record con tipo info errato - record " & i) 'ogni blocco informazioni inizia con un tipo informazione 1 se la cella è piena e non c'è un tipo informazione 1 è errore
End If
'preciso che nei blocchi informativi i tipo informazione seguono questo ordine di priorità: 1, 7, 8, 5, 9, 6
'i blocchi informativi possono avere due tipologie di struttura con i tipo informazioni: 1 (1), 7 (1/N), 8 (0/N), 5 (0/N), 9 (0/N) (tra parentesi è indicata la molteplicità dei record possibile per ogni tipo informazione 1/N c'è sempre almeno una volta, 0/N il tipo informazione può anche non esserci
'oppure: 1 (1), 7 (0/N), 9 (0/N), 6 (1/N) (tra parentesi è indicata la molteplicità dei record possibile per ogni tipo informazione 1/N c'è sempre almeno una volta, 0/N il tipo informazione può anche non esserci
'detto questo procedo nel seguito, sulla base delle diverse combinazioni possibili attivate con if e elseif, a costruire i cicli di ordinamento del database con scrittura della nuova versione
If tipo7 > 0 And tipo8 > 0 And tipo5 > 0 And tipo9 > 0 and tipo6 = 0 Then
For a = 1 To tipo7
For b = 1 To tipo8
For c = 1 To tipo5
For d = 1 To tipo9
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo5 - tipo8 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo5 - tipo8 - tipo7 + a, 1)
Sheets(2).Cells(g, 3).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo5 - tipo8 + b, 1)
Sheets(2).Cells(g, 4).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo5 + c, 1)
Sheets(2).Cells(g, 5).Value = Sheets(1).Cells(i - 1 - tipo9 + d, 1)
g = g + 1
Next
Next
Next
Next
ElseIf tipo7 > 0 And tipo8 > 0 And tipo5 > 0 And tipo9 = 0 and tipo6 = 0 Then
For a = 1 To tipo7
For b = 1 To tipo8
For c = 1 To tipo5
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo5 - tipo8 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo5 - tipo8 - tipo7 + a, 1)
Sheets(2).Cells(g, 3).Value = Sheets(1).Cells(i - 1 - tipo5 - tipo8 + b, 1)
Sheets(2).Cells(g, 4).Value = Sheets(1).Cells(i - 1 - tipo5 + c, 1)
g = g + 1
Next
Next
Next
ElseIf tipo7 > 0 And tipo8 > 0 And tipo5 = 0 And tipo9 = 0 and tipo6 = 0 Then
For a = 1 To tipo7
For b = 1 To tipo8
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo8 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo8 - tipo7 + a, 1)
Sheets(2).Cells(g, 3).Value = Sheets(1).Cells(i - 1 - tipo8 + b, 1)
g = g + 1
Next
Next
ElseIf tipo7 > 0 And tipo8 = 0 And tipo5 = 0 And tipo9 = 0 and tipo6 = 0 Then
For a = 1 To tipo7
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo7 + a, 1)
g = g + 1
Next
ElseIf tipo7 > 0 And tipo8 = 0 And tipo5 > 0 And tipo9 > 0 and tipo6 = 0 Then
For a = 1 To tipo7
For c = 1 To tipo5
For d = 1 To tipo9
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo5 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo5 - tipo7 + a, 1)
Sheets(2).Cells(g, 4).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo5 + c, 1)
Sheets(2).Cells(g, 5).Value = Sheets(1).Cells(i - 1 - tipo9 + d, 1)
g = g + 1
Next
Next
Next
ElseIf tipo7 > 0 And tipo8 = 0 And tipo5 = 0 And tipo9 > 0 and tipo6 = 0 Then
For a = 1 To tipo7
For d = 1 To tipo9
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo7 + a, 1)
Sheets(2).Cells(g, 5).Value = Sheets(1).Cells(i - 1 - tipo9 + d, 1)
g = g + 1
Next
Next
ElseIf tipo7 > 0 And tipo8 > 0 And tipo5 = 0 And tipo9 > 0 and tipo6 = 0 Then
For a = 1 To tipo7
For b = 1 To tipo8
For d = 1 To tipo9
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo8 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo8 - tipo7 + a, 1)
Sheets(2).Cells(g, 3).Value = Sheets(1).Cells(i - 1 - tipo9 - tipo8 + b, 1)
Sheets(2).Cells(g, 5).Value = Sheets(1).Cells(i - 1 - tipo9 + d, 1)
g = g + 1
Next
Next
Next
ElseIf tipo7 > 0 And tipo8 = 0 And tipo5 > 0 And tipo9 = 0 and tipo6 = 0 Then
For a = 1 To tipo7
For c = 1 To tipo5
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo5 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo5 - tipo7 + a, 1)
Sheets(2).Cells(g, 4).Value = Sheets(1).Cells(i - 1 - tipo5 + c, 1)
g = g + 1
Next
Next
ElseIf tipo7 > 0 And tipo9 > 0 And tipo6 > 0 and tipo5 = 0 and tipo8 = 0 Then
For a = 1 To tipo7
For d = 1 To tipo9
For e = 1 To tipo6
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo6 - tipo9 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo6 - tipo9 - tipo7 + a, 1)
Sheets(2).Cells(g, 5).Value = Sheets(1).Cells(i - 1 - tipo6 - tipo9 + d, 1)
Sheets(2).Cells(g, 6).Value = Sheets(1).Cells(i - 1 - tipo6 + e, 1)
g = g + 1
Next
Next
Next
ElseIf tipo7 > 0 And tipo9 = 0 And tipo6 > 0 and tipo5 = 0 and tipo8 = 0 Then
For a = 1 To tipo7
For e = 1 To tipo6
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo6 - tipo7, 1)
Sheets(2).Cells(g, 2).Value = Sheets(1).Cells(i - 1 - tipo6 - tipo7 + a, 1)
Sheets(2).Cells(g, 6).Value = Sheets(1).Cells(i - 1 - tipo6 + e, 1)
g = g + 1
Next
Next
ElseIf tipo7 = 0 And tipo9 > 0 And tipo6 > 0 and tipo5 = 0 and tipo8 = 0 Then
For d = 1 To tipo9
For e = 1 To tipo6
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo6 - tipo9, 1)
Sheets(2).Cells(g, 3).Value = Sheets(1).Cells(i - 1 - tipo6 - tipo9 + d, 1)
Sheets(2).Cells(g, 5).Value = Sheets(1).Cells(i - 1 - tipo6 + e, 1)
g = g + 1
Next
Next
ElseIf tipo7 = 0 And tipo9 = 0 And tipo6 > 0 and tipo5 = 0 and tipo8 = 0 Then
For e = 1 To tipo6
Sheets(2).Cells(g, 1).Value = Sheets(1).Cells(i - 1 - tipo6, 1)
Sheets(2).Cells(g, 6).Value = Sheets(1).Cells(i - 1 - tipo6 + e, 1)
g = g + 1
Next
Else
messaggio = MsgBox("Registrazione non conforme", 0, "Record con tipo info errato - record " & i - 1 - tipo5 - tipo6 - tipo7 - tipo8 - tipo9)
End If
Loop
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub |