Replace your btnAddPlayers with the code below.
Private Sub btnAddPlayers_Click()
Dim iColumn As Long
Dim iLooper As Long
If lstSelectedPlayers.ListCount = 0 Then Exit Sub
On Error Resume Next
With Worksheets(shName)
iColumn = .Cells(1).EntireRow.Find("W" & Right(Me.CbxWeek.Value, 1), , xlValues, xlWhole, xlByColumns, False).Column
End With
If Err.Number <> 0 Then Err.Clear: Exit Sub
If CheckIfDataAlreadyEntered(iColumn, Me.CbxTeam.Value) > 0 Then
MsgBox "Your message"
Exit Sub
End If
' Uses 2nd Column in the Listbox that stores the row that Player is on
With lstSelectedPlayers
For iLooper = 0 To .ListCount - 1
Cells(.List(iLooper, 1), iColumn) = iLooper + 1
Next
.Clear
Label2.Caption = "Count :" & .ListCount
End With
End Sub
Add this code in Standard module where you have your SmileyFace1_Click
Function CheckIfDataAlreadyEntered(WhatColumn As Long, WhatTeam As String) As Long
Dim vaValues As Variant
Dim vaFilter As Variant
Dim lFirst As Long
Dim lLast As Long
With Application.WorksheetFunction
'Get a 1-d array from a column
vaValues = .Transpose(Range(Cells(1, "C"), Cells(Rows.CountLarge, "C")).Value)
'Use match to get the first instance
lFirst = .Match(WhatTeam, vaValues, False)
'Filter on the values
vaFilter = Filter(vaValues, WhatTeam)
'Assumes they're all together
lLast = lFirst + UBound(vaFilter)
CheckIfDataAlreadyEntered = .CountA(Range(Cells(lFirst, WhatColumn).Address, Cells(lLast, WhatColumn).Address))
End With
End Function
Bookmarks