Hello all, I'm wanting to have a system where a user presses a button that opens up a prompt to scan product and if it exists on list B than it copies some data to Sheet 1 A and B. But if it is already on Sheet 1 in A or B to delete it. I have gotten pretty much everything working.
The thing is I have it so it will add everything using any empty spaces first, and if it is on the list it will also be removed. But the problem is, is I have no idea how to make it do a VLOOKUP to see if that product already exists or not.
Take for instance I add the following parts
Glue
Staples
High powered rifle
If I scan Glue again it removes it just fine, but if I scan a high powered rifle it will add it to the first slot despite already being on the list.
*Note these are not my actual products*
Some script I already have
Private Sub CommandButton1_Click()
Dim product As String 'Sets product number as a string
Dim Sheet2Product As String
Dim i As Integer
Dim match As Boolean
Dim rng As Range
Set rng = Sheet1.Range("A:A")
Dim empty1 As Boolean
i = 1
Dim Zone As Integer
Dim duplicate As Boolean
vlookup:
product = InputBox("product Number :") 'Sets what is being checked as an product number
Debug.Print "Get product number"
If IsEmpty(product) = False Then 'makes sure that the field is not empty
Debug.Print "Check if product number is empty"
Sheet2Product = Application.WorksheetFunction.vlookup(product, Sheet2.Range("B:D"), 1, False)
Zone = Application.WorksheetFunction.vlookup(product, Sheet2.Range("B:D"), 2, False)
Debug.Print "Check to see if the product number matches"
End If
If product = Sheet2Product Then
match = True
Else
match = False
End If
DuplicateCheck:
If Sheet2Product = Application.WorksheetFunction.vlookup(product, rng, 1, False) = True Then
duplicate = True
Else
duplicate = False
End If
EmptyCheck:
If IsEmpty(Sheet1.Cells(i, 1)) = True Then
empty1 = True
Else
empty1 = False
End If
If empty1 = False Then
If product = Sheet1.Cells(i, 1) Then
Sheet1.Cells(i, 1) = ""
Sheet1.Cells(i, 2) = ""
MsgBox ("Removed " & product)
Else
i = i + 1
GoTo EmptyCheck
End If
End If
If empty1 = True Then
Sheet1.Cells(i, 1) = product
Sheet1.Cells(i, 2) = Zone
MsgBox ("Added " & product)
End If
Exit Sub
myerror:
MsgBox ("Sheet2Product Not found!")
End Sub
Also please keep in mind I only started learning this about 15-16 hours ago so I'm probably doing everything inefficiently
Bookmarks