I have a table called TablaEntradas in the Entradas tab where all the fields of the userform frmEntradas are stored.
I have another table called Salidas in the Salidas tab where all the fields of the userform frmSalidas are stored.
Both tables have the same number of columns with the same names.
This is what I need:
When I generate an output and it is stored in the TablaSalidas I need it to update the stock of that item in the listaProductos tab, in the TablaProductos table.
Each item that I generate has its specific code, i.e. B0001, B0002, B0003 and so on.
I need the macro to do the following: Subtract the quantity of the item that was recorded in the Entradas minus the quantity of the item that came out in the Salidas. This can be done by verifying that the code (example B0001) is the same in both tables. The answer should be stored in the table TableProducts of the tab listaProductos.
The answer should look like this: Column 1 (Codigo) Column 2 (Division) Column 3 (Item) Column 4 (Existencias) The information of the first 3 columns can be taken from the table TablaSalidas since this table has these columns with this information and these same names.
Code that I have used but it does not work:
Sub ActualizarExistencias()
Dim wsEntradas As Worksheet
Dim wsSalidas As Worksheet
Dim wsProductos As Worksheet
Dim tblEntradas As ListObject
Dim tblSalidas As ListObject
Dim tblProductos As ListObject
Dim rngSalidas As Range
Dim cell As Range
Dim codItem As String
Dim existenciaEntrada As Double
Dim existenciaSalida As Double
Dim filaProducto As ListRow
' Establecer las hojas de trabajo
Set wsEntradas = ThisWorkbook.Sheets("Entradas")
Set wsSalidas = ThisWorkbook.Sheets("Salidas")
Set wsProductos = ThisWorkbook.Sheets("listaProductos")
' Establecer las tablas
Set tblEntradas = wsEntradas.ListObjects("TablaEntradas")
Set tblSalidas = wsSalidas.ListObjects("TablaSalidas")
Set tblProductos = wsProductos.ListObjects("TablaProductos")
' Recorrer cada fila en la tabla de salidas
For Each filaSalida In tblSalidas.ListRows
codItem = filaSalida.Range.Cells(1, 1).Value ' Suponiendo que la columna Código es la primera
' Calcular las existencias
existenciaEntrada = WorksheetFunction.SumIf(tblEntradas.ListColumns(1).DataBodyRange, codItem, tblEntradas.ListColumns("Cantidad").DataBodyRange)
existenciaSalida = WorksheetFunction.SumIf(tblSalidas.ListColumns(1).DataBodyRange, codItem, tblSalidas.ListColumns("Cantidad").DataBodyRange)
existenciaFinal = existenciaEntrada - existenciaSalida
' Buscar la fila correspondiente en la tabla de productos y actualizar la columna Existencias
For Each filaProducto In tblProductos.ListRows
If filaProducto.Range.Cells(1, 1).Value = codItem Then
filaProducto.Range.Cells(1, 4).Value = existenciaFinal ' Columna Existencias
Exit For
End If
Next filaProducto
Next filaSalida
End Sub
Connect the macro to a button I have in the listaProductos tab but it does nothing when I click it.
Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1