Sub ModificaDati()
Dim Shr As Worksheet
Dim Shd As Worksheet
Dim CodiceMod As Long, n As Long, TrovatoRec As Boolean
Dim i As Long, LastRow As Long, LastRowDes As Long
Dim Foglio As String
Set Shr = Sheets("FoglioRiepilogo")
CodiceMod = Application.InputBox("Quale record vuoi modificare?", Type:=1)
LastRow = Shr.Cells(Rows.Count, 5).End(xlUp).Row ' ultima riga piena foglio di riepilogo colonna E
TrovatoRec = False
For i = 5 To LastRow
If Shr.Cells(i, 9).Value = "Reg" & CodiceMod Then
Foglio = Shr.Cells(i, 10)
Set Shd = Sheets(Foglio)
LastRowDes = Shd.Cells(Rows.Count, 5).End(xlUp).Row
For n = 12 To LastRowDes
If "Reg" & CodiceMod = Shd.Cells(n, 9) Then
If Shd.Cells(n, 5) = Shr.Cells(i, 5) Then
If MsgBox("Vuoi Modificare il record ? " & "Reg" & CodiceMod, vbYesNo + vbCritical, "Modifica Record") = vbNo Then GoTo Fine
Shr.Range(Cells(i, 1), Cells(i, 9)).Copy Destination:=Shd.Cells(n, 1)
TrovatoRec = True
MsgBox "Modifica effettuata"
GoTo Fine
Else
Dim Rng As Range, Trovato As Range
Set Rng = Sheets("Foglio1").Range("B8:B15", "F8:F15")
Foglio = Shr.Cells(i, 5)
If Foglio <> "" Then
Set Trovato = Rng.Find(Foglio, LookAt:=xlWhole, LookIn:=xlValues)
If Not Trovato Is Nothing Then
If MsgBox("Vuoi Modificare il record ? " & "Reg" & CodiceMod, vbYesNo + vbCritical, "Modifica Record") = vbNo Then GoTo Fine
Shd.Cells(n, 1).EntireRow.Delete
Set Shd = Sheets(Trovato.Offset(0, 1).Value)
LastRowDes = Shd.Cells(Rows.Count, 5).End(xlUp).Row + 1 ' ultima riga piena +1 foglio destinazione
Shr.Range(Cells(i, 1), Cells(i, 9)).Copy Destination:=Shd.Cells(LastRowDes, 1)
TrovatoRec = True
Shr.Cells(i, 10) = Shd.Name
MsgBox "Modifica effettuata"
End If
End If ' in caso inserire msgbox Foglio non trovato
End If
End If
Next n
End If
Next i
If TrovatoRec = False Then MsgBox "Record o CM non trovato"
Fine:
Set Shr = Nothing
Set Shd = Nothing
Set Trovato = Nothing
End Su |