Private Sub ConfInsert_Click()
Dim sh As Worksheet
Dim rng As Range
Dim database As Workbook
Dim db As Worksheet
Set database = Workbooks("DATAtest.xls")
Set sh = database.Sheets("DB")
sh.Activate
If CodArt.Value = "" Then
MsgBox ("INSERIRE UN CODICE ARTICOLO")
InserisciArticolo.CodArt.SetFocus
Else
sh.Activate
With Workbooks("DATAtest.xls").Worksheets("DB")
Set rng = [a:a].Find(CodArt, lookat:=xlWhole)
MsgBox (rng)
End With
If rng Is Nothing Then
MsgBox ("Codice già presente!")
CodArt.SetFocus
Else
With sh 'assegno i valori delle textbox alle celle'
.Unprotect "0000"
.Range("a1").EntireRow.Insert
.Range("a1").Value = InserisciArticolo.CodArt.Value
.Range("b1").Value = InserisciArticolo.Categoria.Value
.Range("c1").Value = InserisciArticolo.Nome.Value
.Range("d1").Value = InserisciArticolo.ImpForn.Value
.Range("e1").Value = InserisciArticolo.Impposa.Value
.Range("f1").Value = InserisciArticolo.um.Value
.Range("g1").Value = InserisciArticolo.Descforn.Value
.Range("h1").Value = InserisciArticolo.Descfornposa.Value
'Dim lng As Long 'FUNZIONE RIORDINA e inserisci riga
'sh.Activate
'sh.Select
'Columns("A:H").Select
'Selection.Sort Key1:=Range("b1"), Order1:=xlAscending, Key2:=Range("c1"), _
'Order2:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
'Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:= _
'xlSortNormal
For lng = .Range("b" & .Rows.Count).End(xlUp).Row To 2 Step -1 'ciclo le celle interessate partendo dallultima
If .Cells(lng, 2).Value <> .Cells(lng - 1, 2).Value Then 'se il contenuto della cella superiore è diverso
.Cells(lng, 2).EntireRow.Insert Shift:=xlDown 'inserisco una riga
End If
Next
End With
MsgBox ("Articolo inserito correttamente")
End If
sh.Protect DrawingObjects:=False, Contents:=False, Scenarios:=False, AllowFormattingColumns:=True, AllowFormattingRows:=True
Set db = Nothing 'Set a Nothing delle variabili oggetto
Set database = Nothing
End If
End Sub |