Hello
I have a userform and various tables
the userform has 3 listboxes lbxANADate, lbxANAName, and lbxANAShift . lbxANAName is multioption
the form asks for a date, shift time and Name
there can be more than one name for each shift
the code below works really well. But is there a way to check if the name already exists in the lbxANAName column and give a warning that stops a duplicate from happening allowing the user to reenter another name?
i also then want to sort by the column that lbxANAShift data is placed in.
I have a userform and various tables
the userform has 3 listboxes lbxANADate, lbxANAName, and lbxANAShift . lbxANAName is multioption
the form asks for a date, shift time and Name
there can be more than one name for each shift
the code below works really well. But is there a way to check if the name already exists in the lbxANAName column and give a warning that stops a duplicate from happening allowing the user to reenter another name?
i also then want to sort by the column that lbxANAShift data is placed in.
VBA Code:
Private Sub btnANAOk_Click()
Dim sDate As String, sShift As String
Dim i As Long, col As Long, lr As Long
Dim f As Range
Dim Sh As Worksheet
If lbxANADate.ListIndex = -1 Then
MsgBox "Select date"
Exit Sub
End If
If lbxANANames.ListIndex = -1 Then
MsgBox "Select Name"
Exit Sub
End If
If lbxANAShift.ListIndex = -1 Then
MsgBox "Select Shifts"
Exit Sub
End If
Set Sh = Sheets("ANAES Data Entry")
sDate = Format(CDate(lbxANADate.value), "ddd dd/mm")
sShift = lbxANAShift.value
Set f = Sh.Rows(8).Find(sDate, , xlValues, xlPart, , , False)
If Not f Is Nothing Then
col = f.Column
lr = Sh.Columns(col).Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
For i = 0 To lbxANANames.ListCount - 1
If lbxANANames.Selected(i) Then
Sh.Cells(lr, col).value = sShift
Sh.Cells(lr, col + 2).value = lbxANANames.List(i, 0)
lr = lr + 1
End If
Next
Else
MsgBox "Date " & sDate & " does not exist"
End If
End Sub
Last edited by a moderator: