Sub A_ImmagazzinaDatiInDB()
Dim ErroreMov
Dim ErroreDati As Integer
Dim ErroreVal As String, ErroreDocum As String, ErroreDB As String, ErroreIDArt As String
Dim tb As Worksheet
Dim i As Byte, NumReg As Byte
Dim NumRigDB As Long
'On Error Resume Next 'VERIFICARE
Application.ScreenUpdating = False
Sheets("Registrazione").Select
ErroreMov = Worksheets("Registrazione").Range("C7")
ErroreDati = Application.WorksheetFunction.CountA(Worksheets("Registrazione").Range("C9:E108"))
NumReg = Worksheets("Registrazione").Range("H2")
ErroreVal = Worksheets("Registrazione").Range("I2")
ErroreDocum = Worksheets("Registrazione").Range("I3")
ErroreDB = Worksheets("Registrazione").Range("I4")
ErroreIDArt = Worksheets("Registrazione").Range("I5")
If ErroreMov = "" Then msgbox "Attenzione: inserire N° di movimento", vbCritical, "Errore: N° Mov Assente": Exit Sub
If ErroreDati = 0 Then msgbox "Attenzione: dati assenti", vbCritical, "Errore: Dati Assenti": Exit Sub
If ErroreDati Mod 3 <> 0 Then msgbox "Attenzione: dati incompleti", vbCritical, "Errore: Dati Incompleti": Exit Sub
If ErroreVal = "Errori in Registrazione" Then msgbox "Attenzione: presenza di valori errati in Registrazione", vbCritical, "Errore: Valori errati": Exit Sub
If ErroreDocum = "Errori in Docum" Then msgbox "Attenzione: presenza di valori errati in Docum", vbCritical, "Errore: Valori errati": Exit Sub
If ErroreDB = "Dati DB incongrui" Then msgbox "Attenzione: valori in DB cancellati o superflui", vbCritical, "Errore: Valori DB incongrui": Exit Sub
If ErroreIDArt = "Dati IDArticoli incongrui" Then msgbox "Attenzione: valori in IDArticoli cancellati o incompleti", vbCritical, "Errore: Valori IDArticoli incongrui": Exit Sub
Sheets("DB").Unprotect
If Worksheets("DB").FilterMode = True Then
Worksheets("DB").ShowAllData
End If
Sheets("Registrazione").Select
Sheets("Registrazione").Unprotect
' Codice nascondi righe
Set tb = Sheets("Registrazione")
For i = 8 To 108
If IsEmpty(tb.Cells(i, 3)) Then tb.Rows(i).EntireRow.Hidden = True
Next i
' Immagazzina gli ART di "Registrazione" in DB
Range("I9:U108").Select
Selection.SpecialCells(xlCellTypeVisible).Copy
Sheets("DB").Select
Cells(65536, 1).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
' Converte/Ripulisce celle-testo in numero
Range("A1").Select
Cells.Replace What:="", Replacement:="!&/", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
Cells.Replace What:="!&/", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
Cells.Replace What:=" ", Replacement:=" ", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False
Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
' Ripristina tutte le righe dopo stampa
tb.Rows("8:108").EntireRow.Hidden = False
tb.Activate
' Pulisce i vecchi dati Registrazione
Range("C7,C9:F108").ClearContents
Application.CutCopyMode = False
' AggiornaPrezzoMatrice
NumRigDB = Application.WorksheetFunction.CountA(Worksheets("DB").Range("A:A"))
Range("E5").FormulaArray = "=IF(R[-3]C[3]>0,MAX(IF(DB!R[-3]C[1]:R" & NumRigDB & "C[1]=R[-2]C[3],IF(DB!R[-3]C[4]:R" & NumRigDB & "C[4]>0,DB!R[-3]C[7]:R" & NumRigDB & "C[7]/DB!R[-3]C[4]:R" & NumRigDB & "C[4]))),"""")"
Range("E6").FormulaArray = "=IF(R[-4]C[3]>0,MIN(IF(DB!R[-4]C[1]:R" & NumRigDB & "C[1]=R[-3]C[3],IF(DB!R[-4]C[4]:R" & NumRigDB & "C[4]>0,DB!R[-4]C[7]:R" & NumRigDB & "C[7]/DB!R[-4]C[4]:R" & NumRigDB & "C[4]))),"""")"
Application.Goto Reference:=Worksheets("Registrazione").Range("C7"), Scroll:=True
Worksheets("Registrazione").Protect
Sheets("DB").Select
Range("A1").Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Application.Goto Reference:=Worksheets("DB").Cells(65536, 1).End(xlUp).Offset(-NumReg + 1, 0), Scroll:=True
Cells(65536, 1).End(xlUp).Offset(1, 0).Select
Sheets("DB").Protect
End Sub