0

What I am trying to do is check if a row of data exists in multiple columns and then if it doesnt exist insert it. if it does exist, give a message to the user. I have been looking at other pages and I based by code on the accepted answer on this thread: Check Value Exists - Stack Overflow

Dim RowCount As Long
Dim i As Integer
Dim exists As Boolean
exists = False
RowCount = Worksheets("Jobs").Range("A1").CurrentRegion.Rows.Count

For i = 1 To RowCount
    If Worksheets("Jobs").Cells(i, 2).Value = Me.ProjectNumberInput.Value Then
        If Worksheets("Jobs").Cells(i, 3).Value = Me.ProjectNameInput.Value Then
            If Worksheets("Jobs").Cells(i, 4).Value = Me.TaskNumberInput.Value Then
                If Worksheets("Jobs").Cells(i, 5).Value = Me.TaskNameInput.Value Then
                    If Worksheets("Jobs").Cells(i, 6).Value = Me.CompanyNameInput.Value Then
                        If Worksheets("Jobs").Cells(i, 7).Value = Me.ProjectManagerInput.Value Then
                            exists = True
                            Exit For
                        End If
                    End If
                End If
            End If
        End If
    End If
Next i

If exists = False Then
    With Worksheets("Jobs").Range("A1")
        .Offset(RowCount, 0).Value = RowCount
        .Offset(RowCount, 1).Value = Me.ProjectNumberInput.Value
        .Offset(RowCount, 2).Value = Me.ProjectNameInput.Value
        .Offset(RowCount, 3).Value = Me.TaskNumberInput.Value
        .Offset(RowCount, 4).Value = Me.TaskNameInput.Value
        .Offset(RowCount, 5).Value = Me.CompanyNameInput.Value
        .Offset(RowCount, 6).Value = Me.ProjectManagerInput.Value
    End With
    Unload Me
Else
    MsgBox "This Job Already Exists.", vbExclamation, "Add Job"
    Unload Me
End If

I have 7 columns (A-G), the first row is the titles so insert of rows begins on row 2.

However when I run this twice with the exact same details, I get 2 records the same. What have I done wrong?

EDIT: I have found a better way of doing it:

Set Found = Worksheets("Jobs").Columns("B").Find(what:=Me.ProjectNumberInput.Value, LookIn:=xlValues, lookat:=xlWhole)
If Not Found Is Nothing Then
    Set Found = Worksheets("Jobs").Columns("C").Find(what:=Me.ProjectNameInput.Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not Found Is Nothing Then
        Set Found = Worksheets("Jobs").Columns("D").Find(what:=Me.TaskNumberInput.Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not Found Is Nothing Then
            Set Found = Worksheets("Jobs").Columns("E").Find(what:=Me.TaskNameInput.Value, LookIn:=xlValues, lookat:=xlWhole)
            If Not Found Is Nothing Then
                Set Found = Worksheets("Jobs").Columns("F").Find(what:=Me.CompanyNameInput.Value, LookIn:=xlValues, lookat:=xlWhole)
                If Not Found Is Nothing Then
                    Set Found = Worksheets("Jobs").Columns("G").Find(what:=Me.ProjectManagerInput.Value, LookIn:=xlValues, lookat:=xlWhole)
                    If Not Found Is Nothing Then
                        exists = True
                    End If
                End If
            End If
        End If
    End If
End If
Community
  • 1
  • 1
NoLiver92
  • 882
  • 3
  • 15
  • 39

0 Answers0