Merci à tous pour votre aide et vos commentaires. J'ai trouvé un moyen de le faire en utilisant un gestionnaire d'erreurs et bien que je ne pense pas que la solution soit concise ou jolie, j'ai réussi à le faire fonctionner. Je suis sûr que certains des experts ici seraient en mesure de faire la même chose avec beaucoup moins de code si j'avais pu clarifier mes exigences.
Private Sub txtDirection_AfterUpdate()
On Error GoTo MyerrorHandler:
Dim intMyVal As String
Dim lngLastRow As Long
Dim strRowNoList As String
intMyVal = txtName.Value 'Value to search for, change as required.
lngLastRow = Cells(Rows.Count, "AA").End(xlUp).Row 'Search Column A, change as required.
For Each cell In Range("AA4:AA" & lngLastRow) 'Starting cell is F2, change as required.
If cell.Value = intMyVal Then
If strRowNoList = "" Then
strRowNoList = strRowNoList & cell.Row
Else
strRowNoList = strRowNoList & ", " & cell.Row
End If
End If
Next cell
If txtDirection.Value <> "" Then
Ureg.txtDirection.SetFocus
Select Case txtDirection.Value 'If the user scans in
Case "IN"
Range("A2").Select
ActiveCell.End(xlDown).Select
LastRow = ActiveCell.Row
Cells(LastRow + 1, 1).Value = txtDate.Text
Cells(LastRow + 1, 2).Value = Time
Cells(LastRow + 1, 3).Value = txtName.Text
Cells(LastRow + 1, 4).Value = txtLocation.Text
Cells(LastRow + 1, 5).Value = Range("F1").Value
Cells(LastRow + 1, 6).Value = txtName.Text & txtLocation.Text
Cells(strRowNoList, 28).Value = txtDirection.Text
Cells(strRowNoList, 29).Value = txtDate.Text
Cells(strRowNoList, 30).Value = Time
Cells(strRowNoList, 31).Value = txtLocation.Text
Range("A2").Select
txtDate.Value = Date
txtName.Text = ""
txtLocation.Text = ""
txtDirection.Text = ""
Ureg.txtName.SetFocus
Case "OUT" 'If the user scans OUT
Range("A2").Select
ActiveCell.End(xlDown).Select
LastRow = ActiveCell.Row
Cells(LastRow + 1, 1).Value = txtDate.Text
Cells(LastRow + 1, 2).Value = Time
Cells(LastRow + 1, 3).Value = txtName.Text
Cells(LastRow + 1, 4).Value = txtLocation.Text
Cells(LastRow + 1, 5).Value = Range("F1").Value
Cells(strRowNoList, 28).Value = txtDirection.Text
Cells(strRowNoList, 29).Value = txtDate.Text
Cells(strRowNoList, 30).Value = Time
Cells(strRowNoList, 31).Value = txtLocation.Text
Cells(LastRow + 1, 6).Value = txtName.Text & txtLocation.Text
Range("H2").Select
txtDate.Value = Date
txtName.Text = ""
txtLocation.Text = ""
txtDirection.Text = ""
Ureg.txtName.SetFocus
Case "NEW" 'Extra code if the user is set up as a NEW person No longer needed if the erro handler works.
Range("A2").Select
ActiveCell.End(xlDown).Select
LastRow = ActiveCell.Row
Cells(LastRow + 1, 1).Value = txtDate.Text
Cells(LastRow + 1, 2).Value = Time
Cells(LastRow + 1, 3).Value = txtName.Text
Cells(LastRow + 1, 4).Value = txtLocation.Text
Cells(LastRow + 1, 5).Value = Range("F1").Value
Cells(lngLastRow + 1, 31).Value = txtName.Text
Cells(lngLastRow + 1, 32).Value = "IN"
Cells(lngLastRow + 1, 33).Value = txtDate.Text
Cells(lngLastRow + 1, 34).Value = Time
Cells(lngLastRow + 1, 35).Value = txtLocation.Text
Cells(LastRow + 1, 6).Value = txtName.Text & txtLocation.Text
Range("H2").Select
txtDate.Value = Date
txtName.Text = ""
txtLocation.Text = ""
txtDirection.Text = ""
Ureg.txtName.SetFocus
Case Else 'Message if the user scannes something other than in , out or new.
'MsgBox "Please enter either IN or OUT"
Dim AckTime As Integer, InfoBox As Object
Set InfoBox = CreateObject("WScript.Shell")
'Set the message box to close after 10 seconds
AckTime = 5
Select Case InfoBox.Popup("Please enter either IN or OUT. Please try again. (This window will close automatically after 5 seconds).", _
AckTime, "Inccorect Destination Scanned", 0)
Case 1, -1
Exit Sub
End Select
End Select
End If
With ActiveWorkbook
.SaveCopyAs .Path & "\" & Format(Date, "yyyymmdd") & "-" & [A1] & ".xlsm" 'This will save the sheet evertime a user scan is complete.
'MsgBox strRowNoList
End With
'End Sub
MyerrorHandler: 'This adds the name of the uses to the list in AA if they are not there already and then finishes the same code as above for a booking in. No new user should be scanning out.
If Err.Number = 13 Then
Cells(lngLastRow + 1, 27).Value = txtName.Text
Cells(lngLastRow + 1, 28).Value = "IN"
Cells(lngLastRow + 1, 29).Value = txtDate.Text
Cells(lngLastRow + 1, 30).Value = Time
Cells(lngLastRow + 1, 31).Value = txtLocation.Text
Range("H2").Select
txtDate.Value = Date
txtName.Text = ""
txtLocation.Text = ""
txtDirection.Text = ""
Ureg.txtName.SetFocus
End If
End Sub
Merci encore une fois à tous,
Salut Newguy Merci pour votre réponse. Malheureusement, cela ne fonctionne pas comme prévu, bien que peut-être un peu plus proche que j'avais. Maintenant, si je saisis un nom sur le formulaire de l'utilisateur dans txtName qui est dans la liste dans AA alors il efface tous les noms. Si je saisis un nom qui n'est pas dans la liste AA, alors il remplace tous les noms par le 1 que je viens d'entrer plus 1 ligne (Quel est le seul que je voulais ajouter). Je ne suis pas sûr que ce soit de ma faute si je ne m'explique pas bien. Je veux seulement un nom du formulaire de l'utilisateur au bas de la liste dans la colonne AA s'il n'est pas déjà là si c'est alors ne rien faire d'autre. –