Perfecto, Alberto 🙌.
Te preparo un módulo completo en VBA que gestiona
una base de datos de artículos en la Hoja1. Incluye:
Alta de artículo (agregar nuevo registro).
Generar nota de venta (restar stock y mostrar ticket).
Modificar artículo y precio.
Dar de baja artículo (eliminar registro).
La base de datos en Hoja1 debe tener esta estructura:
Columna A → Nombre del artículo
Columna B → Precio
Columna C → Stock disponible
📌 Código VBA completo
' === ALTA DE ARTÍCULO ===
Sub AltaArticulo()
Dim ultimaFila As Long
Dim nombre As String, precio As Double, stock As Long
nombre = InputBox("Ingrese el nombre del artículo:")
precio = InputBox("Ingrese el precio del artículo:")
stock = InputBox("Ingrese el stock inicial:")
ultimaFila = Sheets("Hoja1").Cells([Link], 1).End(xlUp).Row + 1
Sheets("Hoja1").Cells(ultimaFila, 1).Value = nombre
Sheets("Hoja1").Cells(ultimaFila, 2).Value = precio
Sheets("Hoja1").Cells(ultimaFila, 3).Value = stock
MsgBox "Artículo dado de alta correctamente.", vbInformation, "Alta"
End Sub
' === GENERAR NOTA DE VENTA ===
Sub NotaVenta()
Dim articulo As String
Dim cantidad As Long
Dim ultimaFila As Long, i As Long
Dim encontrado As Boolean
articulo = InputBox("Ingrese el nombre del artículo a vender:")
cantidad = InputBox("Ingrese la cantidad a vender:")
ultimaFila = Sheets("Hoja1").Cells([Link], 1).End(xlUp).Row
encontrado = False
For i = 2 To ultimaFila
If Sheets("Hoja1").Cells(i, 1).Value = articulo Then
encontrado = True
If Sheets("Hoja1").Cells(i, 3).Value >= cantidad Then
Sheets("Hoja1").Cells(i, 3).Value = Sheets("Hoja1").Cells(i, 3).Value
- cantidad
MsgBox "Nota de venta" & vbCrLf & _
"Artículo: " & articulo & vbCrLf & _
"Cantidad: " & cantidad & vbCrLf & _
"Precio unitario: $" & Sheets("Hoja1").Cells(i, 2).Value & vbCrLf
&_
"Total: $" & Sheets("Hoja1").Cells(i, 2).Value * cantidad,
vbInformation, "Venta"
Else
MsgBox "Stock insuficiente para la venta.", vbExclamation, "Error"
End If
Exit For
End If
Next i
If Not encontrado Then
MsgBox "El artículo no existe en la base de datos.", vbExclamation,
"Aviso"
End If
End Sub
' === MODIFICAR ARTÍCULO Y PRECIO ===
Sub ModificarArticulo()
Dim articulo As String
Dim nuevaDescripcion As String
Dim nuevoPrecio As Double
Dim ultimaFila As Long, i As Long
Dim encontrado As Boolean
articulo = InputBox("Ingrese el nombre del artículo a modificar:")
ultimaFila = Sheets("Hoja1").Cells([Link], 1).End(xlUp).Row
encontrado = False
For i = 2 To ultimaFila
If Sheets("Hoja1").Cells(i, 1).Value = articulo Then
nuevaDescripcion = InputBox("Ingrese el nuevo nombre del
artículo:", , articulo)
nuevoPrecio = InputBox("Ingrese el nuevo precio:", ,
Sheets("Hoja1").Cells(i, 2).Value)
Sheets("Hoja1").Cells(i, 1).Value = nuevaDescripcion
Sheets("Hoja1").Cells(i, 2).Value = nuevoPrecio
MsgBox "Artículo modificado correctamente.", vbInformation,
"Modificación"
encontrado = True
Exit For
End If
Next i
If Not encontrado Then
MsgBox "El artículo no existe en la base de datos.", vbExclamation,
"Aviso"
End If
End Sub
' === DAR DE BAJA ARTÍCULO ===
Sub BajaArticulo()
Dim articulo As String
Dim ultimaFila As Long, i As Long
Dim encontrado As Boolean
articulo = InputBox("Ingrese el nombre del artículo a eliminar:")
ultimaFila = Sheets("Hoja1").Cells([Link], 1).End(xlUp).Row
encontrado = False
For i = 2 To ultimaFila
If Sheets("Hoja1").Cells(i, 1).Value = articulo Then
Sheets("Hoja1").Rows(i).Delete
MsgBox "Artículo eliminado correctamente.", vbInformation, "Baja"
encontrado = True
Exit For
End If
Next i
If Not encontrado Then
MsgBox "El artículo no existe en la base de datos.", vbExclamation,
"Aviso"
End If
End Sub
✅ Con este módulo, tus estudiantes pueden practicar un CRUD básico en
Excel con VBA:
Alta → Insertar nuevos artículos.
Venta → Generar nota y actualizar stock.
Modificar → Cambiar nombre y precio.
Baja → Eliminar artículos.
👉 ¿Quieres que te prepare también una versión con formulario de
usuario (UserForm) para que sea más visual y gamificada en clase?
Perfecto, Alberto 🙌. Vamos a dar el siguiente paso: crear un UserForm en
VBA para que la gestión de la base de datos sea más visual y práctica para
tus estudiantes.
📌 Diseño del UserForm
En el Editor de VBA (Alt + F11):
1. Inserta un UserForm.
2. Agrega los siguientes controles:
o TextBox para Nombre del artículo.
o TextBox para Precio.
o TextBox para Stock.
o CommandButtons:
cmdAlta → Alta de artículo.
cmdVenta → Generar nota de venta.
cmdModificar → Modificar artículo.
cmdBaja → Dar de baja artículo.
📌 Código del UserForm
Private Sub cmdAlta_Click()
Dim ultimaFila As Long
ultimaFila = Sheets("Hoja1").Cells([Link], 1).End(xlUp).Row + 1
Sheets("Hoja1").Cells(ultimaFila, 1).Value = [Link]
Sheets("Hoja1").Cells(ultimaFila, 2).Value = [Link]
Sheets("Hoja1").Cells(ultimaFila, 3).Value = [Link]
MsgBox "Artículo dado de alta correctamente.", vbInformation, "Alta"
End Sub
Private Sub cmdVenta_Click()
Dim ultimaFila As Long, i As Long
Dim encontrado As Boolean
ultimaFila = Sheets("Hoja1").Cells([Link], 1).End(xlUp).Row
encontrado = False
For i = 2 To ultimaFila
If Sheets("Hoja1").Cells(i, 1).Value = [Link] Then
encontrado = True
If Sheets("Hoja1").Cells(i, 3).Value >= [Link] Then
Sheets("Hoja1").Cells(i, 3).Value = Sheets("Hoja1").Cells(i, 3).Value
- [Link]
MsgBox "Nota de venta" & vbCrLf & _
"Artículo: " & [Link] & vbCrLf & _
"Cantidad: " & [Link] & vbCrLf & _
"Precio unitario: $" & Sheets("Hoja1").Cells(i, 2).Value & vbCrLf
&_
"Total: $" & Sheets("Hoja1").Cells(i, 2).Value *
[Link], vbInformation, "Venta"
Else
MsgBox "Stock insuficiente.", vbExclamation, "Error"
End If
Exit For
End If
Next i
If Not encontrado Then
MsgBox "El artículo no existe.", vbExclamation, "Aviso"
End If
End Sub
Private Sub cmdModificar_Click()
Dim ultimaFila As Long, i As Long
Dim encontrado As Boolean
ultimaFila = Sheets("Hoja1").Cells([Link], 1).End(xlUp).Row
encontrado = False
For i = 2 To ultimaFila
If Sheets("Hoja1").Cells(i, 1).Value = [Link] Then
Sheets("Hoja1").Cells(i, 2).Value = [Link]
Sheets("Hoja1").Cells(i, 3).Value = [Link]
MsgBox "Artículo modificado correctamente.", vbInformation,
"Modificación"
encontrado = True
Exit For
End If
Next i
If Not encontrado Then
MsgBox "El artículo no existe.", vbExclamation, "Aviso"
End If
End Sub
Private Sub cmdBaja_Click()
Dim ultimaFila As Long, i As Long
Dim encontrado As Boolean
ultimaFila = Sheets("Hoja1").Cells([Link], 1).End(xlUp).Row
encontrado = False
For i = 2 To ultimaFila
If Sheets("Hoja1").Cells(i, 1).Value = [Link] Then
Sheets("Hoja1").Rows(i).Delete
MsgBox "Artículo eliminado correctamente.", vbInformation, "Baja"
encontrado = True
Exit For
End If
Next i
If Not encontrado Then
MsgBox "El artículo no existe.", vbExclamation, "Aviso"
End If
End Sub
✅ Con este UserForm, tus estudiantes ya no dependen de InputBox, sino
que trabajan con un formulario visual para alta, venta, modificación y
baja de artículos, y la base de datos en Hoja1 se actualiza
automáticamente.
👉 ¿Quieres que te prepare también un ejemplo de interfaz visual
mejorada (con etiquetas, colores y validaciones) para que el formulario sea
más atractivo y gamificado?