Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Cómo funciona
Tenemos una base de datos con un lista de productos donde especificamos el código, la
descripción y el precio unitario de cada uno.
Cada producto que vayamos registrando se agregará al ListBox del formulario donde se agregará
su descripción, la cantidad de artículos, el precio unitario y el total.
En la parte inferior habrá dos etiquetas donde visualizaremos la cantidad de artículos y el total de
la compra.
Código vba
Ubicación: frmVenta (formulario).
'---------------------------------------------------------------------------------------
' Module : frmVenta
' Author : MVP Excel, Hecho por ...
' Date : 21/11/2018
' Purpose : Formulario tipo punto de venta para capturar productos.
'---------------------------------------------------------------------------------------
'
Option Explicit
'
Private Sub CommandButton1_Click()
'Declaramos variables
Dim strDescripcion As String
Dim intCantidad As Double
Dim doublePUnitario As Double
Dim intTotal As Double
'
'En caso de error
On Error GoTo ErrorHandler
'
With Application.WorksheetFunction
'
'Usamos BUSCARV para encontrar el detalle del producto
strDescripcion = .VLookup(CInt(Me.TextBox1.Value), PRODUCTOS.Range("A:C"), 2, 0)
'
intCantidad = InputBox(strDescripcion & vbNewLine & vbNewLine & "Ingresa la cantidad.",
"Cantidad", 1)
'
If intCantidad = 0 Then GoTo ErrorHandler
'
'Llenamos el ListBox
'...CÓDIGO
Me.ListBox1.AddItem Me.TextBox1.Value
'
'...DESCRIPCIÓN
ListBox1.List(ListBox1.ListCount - 1, 1) = strDescripcion
'
'...CANTIDAD
ListBox1.List(ListBox1.ListCount - 1, 2) = .Text(intCantidad, "#,##0")
'
'...P.UNITARIO
doublePUnitario = .VLookup(CInt(Me.TextBox1.Value), PRODUCTOS.Range("A:C"), 3, 0)
ListBox1.List(ListBox1.ListCount - 1, 3) = .Text(doublePUnitario, "$#,##0.00;-$#,##0.00")
'
'...TOTAL
intTotal = doublePUnitario * intCantidad
ListBox1.List(ListBox1.ListCount - 1, 4) = .Text(intTotal, "$#,##0.00;-$#,##0.00")
'
'...ETIQUETAS
Me.lblProductos = .Text(CInt(Me.lblProductos) + CInt(intCantidad), "#,##0")
Me.lblTotal = .Text(CDbl(Me.lblTotal) + CDbl(intTotal), "$#,##0.00;-$#,##0.00")
'
Me.TextBox1.Value = ""
Me.TextBox1.SetFocus
'
End With
'
Exit Sub
'
ErrorHandler:
'
MsgBox "Ha ocurrido un error: " & Err.Description, vbExclamation, "EXCELeINFO"
'
Me.TextBox1.Value = ""
Me.TextBox1.SetFocus
'
End Sub
'
Private Sub CommandButton4_Click()
Unload Me
End Sub
'
Private Sub CommandButton5_Click()
Dim i As Variant
Dim j As Variant
Dim TransRowRng As Range
Dim NewRow As Integer
'
With VENTAS
'
For i = Me.ListBox1.ListCount To 1 Step -1
'
Set TransRowRng = ThisWorkbook.Worksheets("VENTAS").Cells(1, 1).CurrentRegion
NewRow = TransRowRng.Rows.Count + 1
.Cells(NewRow, 1).Value = Date
.Cells(NewRow, 2).Value = Me.txtConsec.Value
'
For j = 0 To 4
'
.Cells(NewRow, j + 3).Value = Me.ListBox1.List(Me.ListBox1.ListCount - i, j)
'
Next j
'
Next i
'
End With
'
MsgBox "Registros guardados con éxito.", vbInformation, "EXCELeINFO"
'
Unload Me
'
End Sub
'
Private Sub UserForm_Initialize()
Dim intConsecutivo As String
'
Me.ListBox1.ColumnCount = 5
Me.ListBox1.ColumnWidths = "70 pt; 150 pt; 55 pt; 60 pt; 60 pt"
Me.txtFecha.Value = Date
'
intConsecutivo = VENTAS.Range("I1").Value
'
If intConsecutivo = "CONSECUTIVO" Then
'
Me.txtConsec = 1
'
Else
'
Me.txtConsec = intConsecutivo + 1
'
End If
End Sub