Ciao David,
Prova qualcosa del genere:
- Alt-F11 per aprire l'editor di VBA
- Alt-IM per inserire un nuovo modulo di codice
- Nel nuovo modulo vuoto, incolla il seguente codice:
'=========>>
Option Explicit
'--------->>
Public Function LastRow(SH As Worksheet, _
Optional Rng As Range)
If Rng Is Nothing Then
Set Rng = SH.Cells
End If
On Error Resume Next
LastRow = Rng.Find(What:="*", _
after:=Rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
'<<=========
- Alt-IUper creare una nuova Userform (Userform1)
- Nel modulo di codice della Userform, incolla il seguente codice:
'=========>>
Option Explicit
Option Compare Text
Private WB As Workbook
Private SH As Worksheet
Private RngDataTable As Range
Private arrCalculated As Variant
Public EnableEvents As Boolean
'--------->>
Private Sub UserForm_Initialize()
Dim Rng As Range
Dim Ctrl As MSForms.Control
Dim ResCalculated As Variant
Dim LRow As Long
Dim i As Long, j As Long
Const PrimaRiga As Long = 3
Const CalculatedBoxes As String = "tbxIMP,tbxIVA,tbxTOT"
EnableEvents = False
arrCalculated = Split(CalculatedBoxes, ",")
Set WB = ThisWorkbook
Set SH = WB.Sheets("Magazzino")
With SH
LRow = LastRow(SH, .Columns("A:A"))
Set RngDataTable = .Range("A3:M" & LRow)
End With
For Each Ctrl In Me.Controls
With Ctrl
If TypeName(Ctrl) = "TextBox" Then
ResCalculated = Application.Match(.Name, arrCalculated, 0)
If Not IsError(ResCalculated) Then
With .Object
.Enabled = False
.BackColor = vbYellow
.Value = "0.00"
End With
Else
Select Case Ctrl.Name
Case "tbxData"
.Value = Format(Date, "dd-mm-yy")
Case "tbxQta", "tbxPU"
.Value = "0.000"
Case "tbxSC"
.Value = "0.00"
Case "tbxPerCent"
.Value = "0.0"
End Select
End If
ElseIf TypeName(Ctrl) = "Label" Then
.Caption = Mid(.Name, 4)
With .Object
.ForeColor = vbRed
.Font.Bold = True
.Font.Underline = True
.TextAlign = fmTextAlignRight
End With
ElseIf TypeName(Ctrl) = "CommandButton" Then
.Caption = Replace(Mid(.Name, 4), "_", Space(1))
With .Object
.ForeColor = vbBlue
.Font.Bold = True
End With
End If
End With
Next Ctrl
EnableEvents = True
End Sub
'--------->>
Private Sub TbxQta_Change()
If Me.EnableEvents = False Then
Exit Sub
End If
End Sub
'--------->>
Private Sub tbxPU_Change()
If Me.EnableEvents = False Then
Exit Sub
End If
End Sub
'--------->>
Private Sub tbxSC_Change()
If Me.EnableEvents = False Then
Exit Sub
End If
End Sub
'--------->>
Private Sub tbxBolla_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.tbxBolla
.Value = UCase(.Value)
End With
End Sub
'--------->>
Private Sub tbxUM_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.tbxUM
.Value = UCase(.Value)
End With
End Sub
'--------->>
Private Sub tbxQta_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.tbxQta
.Value = Format(.Value, "0.000")
End With
If IsNumeric(tbxQta.Value) Then
Call UpdateIMP
End If
End Sub
'--------->>
Private Sub tbxPU_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.tbxPU
.Value = Format(.Value, "0.000")
End With
If IsNumeric(tbxPU.Value) Then
Call UpdateIMP
End If
End Sub
'--------->>
Private Sub tbxSC_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.tbxSC
.Value = Format(.Value, "0.00")
End With
If IsNumeric(tbxSC.Value) Then
Call UpdateIMP
End If
End Sub
'--------->>
Private Sub tbxPerCent_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me
With .tbxPerCent
.Value = Format(.Value, "0.00")
End With
.tbxIVA.Value = .tbxIMP.Value * .tbxPerCent.Value
.tbxTOT.Value = CDbl(.tbxIMP.Value) + CDbl(.tbxIVA.Value)
End With
End Sub
'--------->>
Private Sub UpdateIMP()
Dim valIMP As Double
With Me
If .tbxQta = vbNullString Or tbxPU = vbNullString _
Or tbxSC = vbNullString Then
valIMP = "0.00"
Else
valIMP = CDbl(.tbxQta.Value) * CDbl(.tbxPU.Value) _
* CDbl(.tbxSC.Value)
.tbxIMP.Value = Format(valIMP, "0.00")
End If
End With
End Sub
'--------->>
Private Sub cmdEsci_Click()
Unload Me
End Sub
'--------->>
Private Sub cmdImmetti_Record_Click()
Dim Ctrl As MSForms.Control
Dim ResCalculated As Variant
Dim iCtr As Long, iRow As Long
Dim res As Variant
res = MsgBox(Prompt:="Vuoi immettere questo record sul foglio " _
& SH.Name & "?", _
Buttons:=vbYesNo, _
Title:="IMMETTERE RECORD")
If res = vbNo Then
Exit Sub
End If
With SH
iRow = LastRow(SH, SH.Columns("A:A"))
.Rows(iRow - 1).Insert
End With
For Each Ctrl In Me.Controls
With Ctrl
If TypeName(Ctrl) = "TextBox" _
Or TypeName(Ctrl) = "ComboBox" Then
iCtr = iCtr + 1
ResCalculated = Application.Match(.Name, arrCalculated, 0)
If IsError(ResCalculated) Then
If IsNumeric(.Value) Then
SH.Cells(iRow - 1, iCtr).Value = CDbl(.Value)
Else
SH.Cells(iRow - 1, iCtr).Value = .Text
End If
Else
With SH.Cells(iRow - 1, iCtr)
.Formula = .Offset(-1).Formula
End With
End If
End If
End With
Next Ctrl
Me.Controls("tbxData").SetFocus
End Sub
'<<=========
- Alt-Q per chiudere l'editor di VBA e tornare a Excel.
- Salva il file con l'estensione .xlsm
Se non hai familiarità con le macro, ti consiglio il seguente articolo eccellente di Mauro:
http://answers.microsoft.com/it-it/office/wiki/office\_2013\_release-excel/excel-dove-e-come-inserire-il-codice-visual-basic/ed29ee63-a537-4e5d-8631-76766cf40503
Per la Userform stessa, potresti:
- Crearla tu, copiando o modificando la mia, o
- Trascinare una copia della mia nel tuo workbook:
- Alt-F11 per aprire l'editor di VBA
- Ctrl-R per accedere alla finestra Project Explorer ('Gestione progetti')
- Fai clic dx sull'oggetto Userform1 e trascinarlo nel progetto VBA del tuo workbook:
, oppure ...
- Copiare i tuoi dati nel mio file (vedi al di sotto)
Potresti scaricare il mio file di prova David20150911.xlsm a:
http://1drv.ms/1MgOVoA
===
Regards,
Norman