Restar valor después de vlookup vba

Estoy trabajando en un formulario de punto de venta en excel y estoy atorado.

Estoy consultando el stock en una tabla con vlookup en donde el usuario escanea el numero de articulo y la función busca su descripción y el stock.

Después de esto quisiera poder descontar del stock el numero de piezas que estoy dando salida.

(CÓDIGO AL FINAL)

Pienso que se podría hacer que después de vlookup guardar la intentar del stock en una variable y después obtener el valor de la variable para descontar el valor de las piezas ya despachadas antes de agregar el siguiente articulo.

Muchas gracias desde ya por la ayuda.

'ESCANEO DE PRODUCTOS
With Application.WorksheetFunction
Me.TextBox1.SetFocus
'DETALLE DE PRODUCTO
strDescripcion = .VLookup(CDbl(Me.TextBox1.Value), PRODUCTOS.Range("A:K"), 2, 0)
'CONSULTA AL STOCK
stockpr = .VLookup(CDbl(Me.TextBox1.Value), PRODUCTOS.Range("A:K"), 5, 0)
'CONDICION SI HAY EXISTENCIAS
If stockpr = 0 Then
GoTo emptyStock
Else
End If

1 respuesta

Respuesta
2

Supongo que en alguna parte de tu usesrform estás capturando la cantidad que vas a sacar, siendo así, entonces sustituye en el código "cantidad" por el nombre de tu control.

Te anexo un código para realizar el descuento.

Private Sub CommandButton1_Click()
'Por.Dante Amor
    'DESCONTAR SALIDA
    '
    Me.TextBox1.SetFocus
    valor = Me.TextBox1.Value
    If Not IsNumeric(valor) Then
        MsgBox "El dato del textbox1 no es un número"
        Exit Sub
    End If
    valor = CDbl(valor)
    Set b = PRODUCTOS.Range("A").Find(valor, lookat:=xlWhole)
    If b Is Nothing Then
        MsgBox "El dato no se encontró", vbExclamation
        Exit Sub
    End If
    strDescripcion = PRODUCTOS.Cells(b.Row, "B")
    stockpr = PRODUCTOS.Cells(b.Row, "E")
    If stockpr = 0 Then
        MsgBox "El stock está en 0", vbExclamation
        Exit Sub
    End If
    If stockpr < cantidad Then
        MsgBox "El stock es inferior a la cantidad a despachar", vbExclamation
        Exit Sub
    End If
    '
    'Descontar la cantidad del stock
    PRODUCTOS.Cells(b.Row, "E") = PRODUCTOS.Cells(b.Row, "E") - cantidad
    '
    MsgBox "Cantidad descontada del stock", vbInformation, "SALIDAS"
End Sub

.

'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias

.

Feliz Año 2018

.

Hola, me parece excelente la solución pero hay un error que no logro resolver, ¿supongo qué será al declarar la variable "b"? (CÓDIGO ABAJO)

Gracias de antemano.

Private Sub CommandButton1_Click()
'Registrar producto y capturar cantidad
'Declaramos variables
Dim strDescripcion As String
Dim intCantidad As Long
Dim doublePUnitario As Integer
Dim intTotal As Double
Dim stockpr As Integer
Dim umed As String
Dim stockind As String
Dim valor As Double
Dim b As String
'ESCANEO DE PRODUCTOS
With Application.WorksheetFunction
    Me.TextBox1.SetFocus
    valor = Me.TextBox1.Value ' VALOR = CODIGO PRODUCTO
    If Not IsNumeric(valor) Then 'VALIDAR SI ES NUMERO
    MsgBox "El dato del textbox1 no es un número"
    Exit Sub
    End If
    valor = CDbl(valor) 'CONVIERTE A DUBLE VALOR
    b = PRODUCTOS.Range("A").Find(valor, lookat:=xlWhole) ' BUSCAR CODIGO EN TABLA PRODUCTOS
    If b Is Nothing Then
    MsgBox "El dato no se encontró", vbExclamation ' VALIDA QUE EL PRODUCTO EXISTA
    Exit Sub
    End If
    strDescripcion = PRODUCTOS.Cells(b.Row, "B") ' GUARDA LA DESCRIPCION DEL PRODUCTO
    intCantidad = InputBox(strDescripcion & vbNewLine & vbNewLine & "Ingresa la cantidad.", "Cantidad", 1)
    stockpr = PRODUCTOS.Cells(b.Row, "E") ' GUARDA EL STOCK DEL PRODUCTO
    If stockpr = 0 Then ' CONSULTA QUE EL STOCK PERMITA DESPACHAR
    MsgBox "El stock está en 0", vbExclamation
    Exit Sub
    End If
    If stockpr < intCantidad Then ' SEGUNDA CONSULTA AL STOCK
    MsgBox "El stock es inferior a la cantidad a despachar", vbExclamation
    Exit Sub
    End If
    PRODUCTOS.Cells(b.Row, "E") = PRODUCTOS.Cells(b.Row, "E") - intCantidad ' RESTA LA SALIDA DEL STOCK
    umed = InputBox("PZ, GAL, TON, LT, MT, CM, GR, KG." & vbNewLine & vbNewLine & "UNIDAD DE MEDIDA.", "UNIDAD DE MEDIDA", "PZ")

Te faltó poner

set b = 

solamente pusiste 

b =


Consejos sobre variables:

Según su función ¿Cómo declarar variables?

Sal u dos

He puesto el set b = y ahora dice que requiere un objeto.

Disculpa la molestia y mil gracias por la ayuda.

Revisa bien cómo puse mi macro.

Y por otra parte, quita todas las declaraciones de variables.

Y qué es Productos, es el name de la hoja

he declarado como variant la variable "b" y ya no pide objeto ahora sale este error:

Aqui le he dado en depurar:

ya he quitado las declaraciones, el nombre de la hoja es "PRODUCTOS".

ahora dice que no se ha definido la variable "valor"

Cambia esto

Set b = PRODUCTOS.Range("A").Find(valor, lookat:=xlWhole)

Por esto

Set b = PRODUCTOS.Range("A:A").Find(valor, lookat:=xlWhole)

Y no me has contestado qué es PRODUCTOS?

NOTA: Declarar la variable como variant es lo mismo que no declararla.

Tienes esta línea: Option Explicit, también quítala.

PRODUCTOS es el name de la hoja, ¿o es el nombre de usuario de la hoja?

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas