I'm in the process of writing User Form code for a new project. Right now I'm writing the moduel to Add and Delete a product list, but it gives me an application-defined or object-defined error. Does any one know what I have done worng? I have highlighted in red where the error occurs. Hopefully, if I get past this error, my code will delete the product from the list!
Private Sub cmdDelete_Click()
Dim x As Integer
Dim lRow As Long
Dim lProduct As Long
Dim sProduct As String
Dim ws As Worksheet
Set ws = Worksheets("LookupList")
'find first empty row in database
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
lProduct = Me.cboProduct.ListIndex
sProduct = Me.cboProduct.Value
'check for a part number
If Trim(Me.cboProduct.Value) = "" Then
Me.cboProduct.SetFocus
MsgBox "Please enter a Product Number"
Exit Sub
End If
For x = 1 To lProduct
If ws.Cells(x, 0).Value = sProduct Then
ws.Cells(x, 0).EntireRow.Delete
'clear the data
Me.cboProduct.Value = ""
Me.cboProduct.SetFocus
Unload Me
Exit Sub
End If
Next x
'if you get this far then the item you are trying to delete is not in the list. This should never happen!
MsgBox "Item not in list!"
End Sub
Thanks for your help! I have attached a copy of the spreadsheet.
redsab
Bookmarks