Hi,
You can use this VBA code to run a check before saving the data in your Excel macro to prevent saving a product that is already registered in your products table-
Sub SaveProduct() Dim ws As Worksheet Dim productTable As ListObject Dim productName As String Dim productRange As Range ' Set the worksheet containing the products table Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with the actual name of your worksheet ' Set the products table as a ListObject (assuming it's a structured table) Set productTable = ws.ListObjects("Table1") ' Replace "Table1" with the actual name of your table ' Get the product name from the user productName = InputBox("Enter the product name:") ' Check if the product already exists in the table Set productRange = productTable.ListColumns("Product").DataBodyRange If Not IsError(Application.Match(productName, productRange, 0)) Then MsgBox "Product already exists in the table.", vbExclamation Exit Sub End If ' Save the product to the table productTable.ListRows.Add productTable.ListColumns("Product").Range.Cells(productTable.ListRows.Count).Value = productName MsgBox "Product saved successfully.", vbInformation End Sub
You will be prompted to enter the product name by this macro. The "Product" column is then searched for a match to see if the product is already present in the products table. A message is shown and the macro ends without saving if a match is found. If there isn't a match, the item is added to the table and a success message is shown.
But, you'll need to customize the code to match the names of your worksheet, table, and columns accordingly.
Best Regards.