+ Reply to Thread
Results 1 to 3 of 3

Search duplicate before insert data using userform

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-06-2014
    Location
    malaysia
    MS-Off Ver
    Excel 2003/2010/2013
    Posts
    107

    Search duplicate before insert data using userform

    hi, Im creating a excel booklist for our library and im using userform to insert new entry of books details. could someone help me to fix my code to search duplicate for ISBN, title and call number. If this 3 entry no duplicate found, then only create/insert new booklist entry. So far im only managed to get this work for ISBN only.

    Option Explicit
    
    Private Sub CancelButton_Click()
    
    Unload Me
    
    End Sub
    
    
    Private Sub ClearButton_Click()
    
    Call UserForm_Initialize
    
    End Sub
    
    Private Sub OKButton_Click()
    
    Dim FoundCell As Range
    Dim Search As String
    Dim emptyRow As Long
    Dim i As Long
    Dim tmpStr As String
    Dim ws As Worksheet
    
    ''remove text for ISBN column
    tmpStr = ISBNTextBox.Value
        
        For i = 1 To Len(tmpStr)
            Select Case Mid$(tmpStr, i, 1) '//examine current char
                Case "0" To "9" '//permitted chars
                   '//ok
                Case Else
                   Mid$(tmpStr, i, 1) = "!"
            End Select
        Next i
        tmpStr = Replace$(tmpStr, "!", "") '//strip invalids & return
    
    ''check duplicate for ISBN column
    Set ws = Worksheets("booklist")
    emptyRow = ws.Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).Row
    Search = ISBNTextBox.Text
    Set FoundCell = ws.Columns(5).Find(Search, LookIn:=xlValues, Lookat:=xlWhole)
    If FoundCell Is Nothing Then
    MsgBox "No existing ISBN found, inserted new list!"
    
    ''create new entry
    Cells(emptyRow, 1).Value = NoTextBox.Value
    Cells(emptyRow, 2).Value = TitleTextBox.Value
    Cells(emptyRow, 3).Value = AuthorTextBox.Value
    Cells(emptyRow, 4).Value = CopyTextBox.Value
    Cells(emptyRow, 5).Value = tmpStr
    Cells(emptyRow, 6).Value = CallNoTextBox.Value
    Cells(emptyRow, 7).Value = PublicationTextBox.Value
    
    Else
    MsgBox "ISBN exists!" & " data found at cell address " & FoundCell.Address
    End If
    
    If DeptCheckBox1.Value = True Then Cells(emptyRow, 8).Value = DeptCheckBox1.Caption
    
    If DeptCheckBox2.Value = True Then Cells(emptyRow, 8).Value = Cells(emptyRow, 8).Value & " " & DeptCheckBox2.Caption
    
    If DeptCheckBox3.Value = True Then Cells(emptyRow, 8).Value = Cells(emptyRow, 8).Value & " " & DeptCheckBox3.Caption
    
    
    End Sub
    
    Private Sub UserForm_Initialize()
    ''No. coulumn auto numbering
    Dim LstRw As Long
    LstRw = Cells(Rows.Count, "A").End(xlUp).Row
    Me.NoTextBox.Value = Cells(LstRw, "A").Value + 1
    NoTextBox.Value = Me.NoTextBox.Value
    ''Empty TextBox
    TitleTextBox.Value = ""
    AuthorTextBox.Value = ""
    CopyTextBox.Value = ""
    ISBNTextBox.Value = ""
    CallNoTextBox.Value = ""
    PublicationTextBox.Value = ""
    
    DeptCheckBox1.Value = False
    DeptCheckBox2.Value = False
    DeptCheckBox3.Value = False
    
    'Set Focus on NameTextBox
    NoTextBox.SetFocus
    
    End Sub
    https://www.excelforum.com/attachmen...1&d=1580278127
    Attached Files Attached Files
    Last edited by amein; 01-29-2020 at 02:25 AM. Reason: add details

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Search duplicate before insert data using userform

    If you work with the textbox_exit event for each textbox, then you will know if the value exists right away.

    Private Sub TitleTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        Dim x
        x = Application.WorksheetFunction.CountIf(Range("B:B"), Me.TitleTextBox)
        If x > 0 Then
            MsgBox "Title exists, please enter a new title"
        End If
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    03-06-2014
    Location
    malaysia
    MS-Off Ver
    Excel 2003/2010/2013
    Posts
    107

    Re: Search duplicate before insert data using userform

    Quote Originally Posted by davesexcel View Post
    If you work with the textbox_exit event for each textbox, then you will know if the value exists right away.

    Private Sub TitleTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        Dim x
        x = Application.WorksheetFunction.CountIf(Range("B:B"), Me.TitleTextBox)
        If x > 0 Then
            MsgBox "Title exists, please enter a new title"
        End If
    
    End Sub
    Thank you for your reply, I modified little bit and come out with this.

    Private Sub TitleTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        Dim title
        title = Application.WorksheetFunction.CountIf(Range("B:B"), Me.TitleTextBox)
        If title > 0 Then
            Title_checker.Caption = "Duplicate"
        Else
            Title_checker.Caption = ChrW(&H2713)
        End If
    
    End Sub
    Attachment 660266

    Then, now, I want to add a button so that once we found the duplicate, then we can click on that button and directly go to the cell of the duplicate data.

    I try something like this but only works with 1 value which is Title. how to combine this code so that can work with ISBN and Call number too?

    Or can we add into the first code for this purpose?

    Private Sub Gotobutton_Click()
        Dim Finddupe, Rng As Range
        
        Set Rng = Worksheets("booklist").Range("B:B")
        Set Finddupe = Rng.Find(TitleTextBox.Text, LookIn:=xlValues, Lookat:=xlWhole)
        Application.GoTo Worksheets("booklist").Range(Finddupe.Address)
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. USERFORM : Search and edit data in userform
    By mohit.kumar9094 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2018, 07:50 AM
  2. [SOLVED] How can I search a column for duplicate then transfer data from UserForm to next empy row?
    By gmr4evr1 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-11-2016, 10:26 AM
  3. Userform, Search, copy an paste data from a userform database to another sheet.
    By masro in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-15-2015, 02:42 PM
  4. [SOLVED] Userform to search worksheet and display data back to the userform
    By zip247 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2015, 05:46 AM
  5. Replies: 0
    Last Post: 02-27-2014, 04:43 PM
  6. Duplicate a spreadsheet and insert data in it
    By khantouch in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2013, 04:29 AM
  7. Search column a for duplicate data and copy entire row to duplicate sheet
    By crazyAMP in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2012, 08:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1