jeudi 4 juillet 2019

Find function and if elseif to Avoid Duplicates from User Form input

I am really new to VBA and have been struggling with the code for a userform that needs to check the input values before adding the table to avoid duplicates and provide directions in a message box.

When the user clicks the "transfer" command button, I would like the code to first look at WONumber that the user is trying to input see if is already on the table. If the WONumber is not on the table, a msgbox appears telling the user the WONumber does not exist.

If the WONumber is on the table, I want the code to then look at SubName for all the cases where the WONumber matches their input and see if the SubName they are trying to input already exists on the table with the associated WONumber. If the SubName is already on the table with the WONumber, a msgbox appears letting the user know that the SubName is already on the WONumber. If the SubName is on the table but not with the same user input WONumber, the SubName with the associated WONumber is added to the table. Essentially, the WONumber is unique to the table and the SubName is unique to the WONumber but not unique to the table. (The user SHOULD be able add SubName = "Smith" with WONumber = "5" and SubName = "Smith" with WONumber = "500" but SHOULD NOT be able to add SubName = "Smith" with WONumber = "5" twice.)

I've tried multiple combinations of If Not Nothing since I've read that sometimes it's easier to work from that direction.

I have very little experience with VBA in general so I don't know if the .Find returns a value or a location so the ElseIf conditions I have written may not make sense at all since the code might be finding a location and I am asking it to match text.

I've tried FoundWO.Value = PsblWOMatch.Value and FoundSub.Value = FoundSubName.Value as the Elseif conditions as well as .text.

I'm not sure about the dim FoundWO as Range, FoundSub as Range since that seems to imply that FoundWO would be set of values and to equate a set of numbers with one PsblWOMatch really doesn't make sense.

I've also found a few YouTube videos about the specific error code 91 having to do with my code not including dim ws as worksheet that being the issue but when I have tried to correct it to match the example I get the same error code just on a different line.

    Private Sub cmdTransfer_Click()

    Dim FoundWO As Range
    Dim PsblWOMatch As String
    PsblWOMatch = txtWONumber.Text

    Dim FoundSub As Range
    Dim PsblSubMatch As String
    PsblSubMatch = txtSubName.Text

    Dim eRow As Long
    eRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

Set FoundWO = ActiveSheet.ListObjects("TableWO").ListColumns(1).DataBodyRange.Find(What:=PsblWOMatch, LookIn:=xlValues, Lookat:=xlWhole)
Set FoundSub = ActiveSheet.ListObjects("TableWO").ListColumns(2).DataBodyRange.Find(What:=PsblSubMatch, LookIn:=xlValues, Lookat:=xlWhole)

'If Condition 1 then
If FoundWO Is Nothing Then
'Code to execute
   MsgBox "Work Order does not exist. Please add Work Order before adding subcontractors."

'If Condition 2 then
ElseIf FoundWO = PsblWOMatch And FoundSub = PsblSubMatch Then
'Code to execute
    MsgBox "SubContractor has already been added to Work Order."

Else
'Code to execute
    Cells(eRow, 1).Value = txtWONumber.Text
    Cells(eRow, 2).Value = ""
    Cells(eRow, 3).Value = txtSubName.Text
    Cells(eRow, 4).Value = txtsubLocation.Text

End If

txtWONumber.Text = ""
txtSubName.Text = ""
txtsubLocation = ""
txtWONumber.SetFocus

End Sub

I've been able to get the msgbox to appear telling the user that the WONumber does not exist. And I have been able to add a SubName when the WONumber does exist. But I am getting Run time error '91': Object variable or With block variable not set. The debug highlight is on the ElseIf line.

Aucun commentaire:

Enregistrer un commentaire