Option Explicit
Sub Registra()
Dim LR As Integer, MyRange As Range, LO As Integer, Nr As Byte, Risp As Byte
Risp = MsgBox("Sei sicuro di voler registrare l'ordine n. " & [b3] & " del Cliente " & [a2] & "?", Buttons:=vbYesNo)
If Risp = vbYes Then
LR = [a60].End(xlUp).Row
If LR < 10 Then LR = 60
Set MyRange = Range("a10:h" & LR)
Nr = MyRange.Rows.Count - 1 'Numero di Item ordinati
LO = Foglio3.Cells(Rows.Count, 1).End(xlUp).Row + 1
' Faccio dei controlli preliminari
' Prima di registrare l'ordine nuovo mi accerto che non era già stato registrato in precedenza
If Range("b3").Font.Bold = True Then
MsgBox "Commissione già regitrata. Impossibile procedere!!", vbCritical
Exit Sub
End If
' Poi verifico che i campi che mi servono siano stati correttamente completati
If [a10] = "" Or [g2] = "" Or [b3] = "" Or [a2] = "" Or [b65] = "" Then
MsgBox " Ci sono campi obbligatori che sono vuoti. Completa prima di registrare!", vbCritical
Exit Sub
End If
' se supero il controllo posso registrare la nuova commissione
With Foglio3
.Range("a" & LO & ":a" & LO + Nr).Value = [g2] ' CASA Mandante
.Range("b" & LO & ":b" & LO + Nr).Value = [b3] 'N. Commissione
.Range("c" & LO & ":c" & LO + Nr).Value = [a2] 'Ragione Sociale Cliente
.Range("d" & LO & ":d" & LO + Nr).Value = [b65] 'Data Ordine
.Range("d" & LO & ":d" & LO + Nr).NumberFormat = "dd/mm/yy" 'Formatto la data
.Range("e" & LO & ":l" & LO + Nr).Value = MyRange.Value 'il corpo dell'Ordine
End With
MsgBox "Ordine N. " & [b3] & " del Cliente : " & [a2] & ". Registrato!!", vbInformation
Range("b3").Font.Bold = True
Else
MsgBox "Registrazione ANNULLATA", vbExclamation
End If
End Sub |