Consulting

Results 1 to 4 of 4

Thread: Check if data is already exist before adding a new data

  1. #1
    VBAX Regular
    Joined
    Jan 2021
    Posts
    14
    Location

    Check if data is already exist before adding a new data

    Hello! So, I'm currently having a problem in my data entry. Just want to ask if how can I avoid duplicates? So on my worksheet, I have an ADD sheet that the user is going to use to enter the data. Then I have the DATA sheet where all of the data is. The unique key is the STUDENT ID. Thank you in advance!
    Attached Files Attached Files

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub add_stud()
    
        If Application.CountIf(Worksheets("Data").Columns(3), Range("a_studID").Value) > 0 Then
            MsgBox "Existing student ID in database! Quitting...", vbOKOnly, "Duplicate entry!"
            Exit Sub
        End If
        
        With Worksheets("Data")
            next_row = .Range("C" & .Rows.Count).End(xlUp).Offset(1).Row
            .Cells(next_row, 3).Value = Range("a_studID").Value
            .Cells(next_row, 4).Value = Range("a_lname").Value
            .Cells(next_row, 5).Value = Range("a_fname").Value
            .Cells(next_row, 6).Value = Range("a_ext").Value
            .Cells(next_row, 7).Value = Range("a_mname").Value
        End With
        
        ThisWorkbook.Save
        
        MsgBox "Data has been saved!", vbOKOnly + vbInformation, "SF10"
        
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Regular
    Joined
    Jan 2021
    Posts
    14
    Location
    Quote Originally Posted by mancubus View Post
    Sub add_stud()
    
        If Application.CountIf(Worksheets("Data").Columns(3), Range("a_studID").Value) > 0 Then
            MsgBox "Existing student ID in database! Quitting...", vbOKOnly, "Duplicate entry!"
            Exit Sub
        End If
        
        With Worksheets("Data")
            next_row = .Range("C" & .Rows.Count).End(xlUp).Offset(1).Row
            .Cells(next_row, 3).Value = Range("a_studID").Value
            .Cells(next_row, 4).Value = Range("a_lname").Value
            .Cells(next_row, 5).Value = Range("a_fname").Value
            .Cells(next_row, 6).Value = Range("a_ext").Value
            .Cells(next_row, 7).Value = Range("a_mname").Value
        End With
        
        ThisWorkbook.Save
        
        MsgBox "Data has been saved!", vbOKOnly + vbInformation, "SF10"
        
    End Sub
    It worked! Thank you so much!

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •