Userform Text Box Duplicates

Aviles

Board Regular
Joined
Dec 17, 2008
Messages
173
Office Version
  1. 365
Platform
  1. Windows
Hello,
I'm currently using the below code in a userform:

Code:
Sheets("Database").Visible = True
    Sheets("Database").Activate
    NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
    Cells(NextRow, 1) = txtClientFM.Text
    Cells(NextRow, 2) = txtNewPortfolio.Text

...which simply takes the values from the 2 text boxes and places them in the "Database" sheet.

It works fine, but I would like to add to this some code which avoids the user from entering a Portfolio number (in txtNewPortfolio) that already exists in the "Database" sheet... so avoid enetering duplicates. The list of existing Porfolios are on Column B of the "Database" sheet.

If they enter an existing Portfolio, then: MsgBox "This Porfolio code already exists."

Thanks.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You could use CountIf to check for duplicates.
Code:
Dim rngPFolios As Range
Dim LastRow As Long
 
With Worksheets("Database")
 
      LastRow = .Range("B" & Rows.Count).End(xlUp).Row
 
      Set rngPFolios = .Range("B1:B" & LastRow)
 
End With        
 
NoFolios = Application.WorksheetFunction.CountIf(rngPFolios, txtNewPortFolio)
 
If NoFolios>0 Then
 
     Msgbox "This Porfolio code already exists."
 
Else
    Worksheets("Database").Cells(LastRow+1, 1) = txtClientFM.Text
    Worksheets("Database").Cells(LastRow+1, 2) = txtNewPortFolio.Text
End If
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,822
Members
452,946
Latest member
JoseDavid

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