Userform vba preventing duplicates and returning value based on original entry

HRIS

Board Regular
Joined
Dec 29, 2010
Messages
116
I created a userform and the code below attached to it. The userform consists of a text box, a combo box and the OK button. The code inserts a new job title (coming from the text box) at the end of a corresponding list based upon the users combobox selection - which then returns the corresponding job code (from the column to the left of the list) to a cell at the top of the spreadsheet.

I am pretty new to vba and am struggling to figure out how to prevent a duplicate entry within the separate lists. If the user enters a duplicate, I'm wanting it to return the original entries corresponding job code and prevent the entry from being added to the list.

I appreciate any ideas you may have!


Code:
Private Sub OK_Click()
 
        If TextBox1 = vbNullString Then
            MsgBox "You Must Enter A Job Name"
                If vbOK Then
                    Me.TextBox1.SetFocus
                    Cancel = True
                    Exit Sub
                End If
        End If
 
 
        If CreateJobCode = ("KC") Then
            Range("D3").Value = (Range("B7:B65536").SpecialCells(4)(1, 0))
            Range("B7:B65536").SpecialCells(4)(1).Select
            Selection.Value = TextBox1
             Unload Me
 
        ElseIf CreateJobCode = ("KCPM") Then
            Range("D3").Value = (Range("D7:D65536").SpecialCells(4)(1, 0))
            Range("D7:D65536").SpecialCells(4)(1).Select
            Selection.Value = TextBox1
            Unload Me
 
        ElseIf CreateJobCode = ("NEO") Then
            Range("D3").Value = (Range("F7:F65536").SpecialCells(4)(1, 0))
            Range("F7:F65536").SpecialCells(4)(1).Select
            Selection.Value = TextBox1
            Unload Me
 
        ElseIf CreateJobCode = ("NEOPM") Then
            Range("D3").Value = (Range("H7:H65536").SpecialCells(4)(1, 0))
            Range("H7:H65536").SpecialCells(4)(1).Select
            Selection.Value = TextBox1
            Unload Me
 
        ElseIf CreateJobCode = ("Non-Employee") Then
            Range("D3").Value = (Range("J7:J65536").SpecialCells(4)(1, 0))
            Range("J7:J65536").SpecialCells(4)(1).Select
            Selection.Value = TextBox1
            Unload Me
 
        Else
            MsgBox "Please Select An Option"
                If vbOK Then
                    Me.CreateJobCode.SetFocus
                End If
 
 
        End If
 
    Range("D3").Select
 
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Figured out a method to do what I was needing - so thought I would post my code in case it could be helpful to someone in the future or in case someone sees a better way to do this!

Code:
Private Sub OK_Click()
 
        If TextBox1 = vbNullString Then
            MsgBox "You Must Enter A Job Name"
                If vbOK Then
                    Me.TextBox1.SetFocus
                    Cancel = True
                    Exit Sub
                End If
        End If
 
 
        If CreateJobCode = ("KC") Then
            Range("D3").Value = Application.Index(Range("A7:B65536"), Application.Match(TextBox1, Range("B7:B65536"), 0), 1)
                If IsError(Range("D3").Value) Then
                    Range("D3").Value = (Range("B7:B65536").SpecialCells(4)(1, 0))
                    Range("B7:B65536").SpecialCells(4)(1).Select
                    Selection.Value = TextBox1
                End If
            Unload Me
 
        ElseIf CreateJobCode = ("KCPM") Then
            Range("D3").Value = Application.Index(Range("C7:D65536"), Application.Match(TextBox1, Range("D7:D65536"), 0), 1)
                If IsError(Range("D3").Value) Then
                    Range("D3").Value = (Range("D7:D65536").SpecialCells(4)(1, 0))
                    Range("D7:D65536").SpecialCells(4)(1).Select
                    Selection.Value = TextBox1
                End If
            Unload Me
 
        ElseIf CreateJobCode = ("NEO") Then
            Range("D3").Value = Application.Index(Range("E7:F65536"), Application.Match(TextBox1, Range("F7:F65536"), 0), 1)
                If IsError(Range("D3").Value) Then
                    Range("D3").Value = (Range("F7:F65536").SpecialCells(4)(1, 0))
                    Range("F7:F65536").SpecialCells(4)(1).Select
                    Selection.Value = TextBox1
                End If
            Unload Me
 
        ElseIf CreateJobCode = ("NEOPM") Then
            Range("D3").Value = Application.Index(Range("G7:H65536"), Application.Match(TextBox1, Range("H7:H65536"), 0), 1)
                If IsError(Range("D3").Value) Then
                    Range("D3").Value = (Range("H7:H65536").SpecialCells(4)(1, 0))
                    Range("H7:H65536").SpecialCells(4)(1).Select
                    Selection.Value = TextBox1
                End If
            Unload Me
 
         ElseIf CreateJobCode = ("Non-Employee") Then
            Range("D3").Value = Application.Index(Range("I7:J65536"), Application.Match(TextBox1, Range("J7:J65536"), 0), 1)
                If IsError(Range("D3").Value) Then
                    Range("D3").Value = (Range("J7:J65536").SpecialCells(4)(1, 0))
                    Range("J7:J65536").SpecialCells(4)(1).Select
                    Selection.Value = TextBox1
                End If
            Unload Me
 
         Else
            MsgBox "Please Select An Option"
                If vbOK Then
                    Me.CreateJobCode.SetFocus
                End If
 
 
        End If
 
    Range("D3").Select
 
 
End Sub
 
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top