وعليكم السلام ورحمة الله تعالى وبركاته
جرب إفراغ اليوزرفورم من جميع الأكواد السابقة ولصق الكود التالي ربما يناسبك
Option Explicit
Public Property Get WS() As Worksheet: Set WS = Sheets("RECAP MDN+DGSN"): End Property
Private Sub CommandButton1_Click()
Const MAX_DAYS As Long = 90
Dim a As Variant, matricule As String, xDate As Date, lastDate As Date
Dim i As Long, tmp As Long, trouve As Boolean, jRestants As Long
matricule = Trim(Me.TextBox2.Value)
If matricule = "" Then MsgBox "المرجو إدخال رقم التسجيل", vbExclamation, "تنبيه": Exit Sub
If Not IsDate(Me.TextBox3.Value) Then MsgBox "المرجو إدخال التاريخ", vbExclamation, "خطأ": Exit Sub
xDate = CDate(Me.TextBox3.Value): a = WS.Range("B8:C22").Value
For i = UBound(a, 1) To 1 Step -1
If Trim(a(i, 1)) = matricule And IsDate(a(i, 2)) Then lastDate = a(i, 2): trouve = True: Exit For
Next i
If trouve And xDate - lastDate < MAX_DAYS Then
jRestants = MAX_DAYS - (xDate - lastDate)
MsgBox "يوجد تسجيل سابق بتاريخ: " & Format(lastDate, "dd/mm/yyyy") & vbCrLf & _
"يرجى الانتظار " & jRestants & " يوم قبل التسجيل مجددا", vbExclamation, "تنبيه"
Exit Sub
End If
For i = 1 To UBound(a, 1)
If Trim(a(i, 1)) = "" Then tmp = i: Exit For
Next i
If tmp = 0 Then MsgBox "النطاق ممتلئ لا يمكن إضافة تسجيل جديد", vbCritical, "خطأ": Exit Sub
a(tmp, 1) = matricule: a(tmp, 2) = xDate
WS.Range("B8:C22").Value = a
MsgBox "تمت إضافة التسجيل بنجاح", vbInformation
Me.TextBox2.Value = "": Me.TextBox3.Value = ""
End Sub
'====================
Private Sub CommandButton4_Click()
Dim OnRng As Variant, matricule As String, tmps As Date
Dim i As Long, supprimé As Boolean
matricule = Trim(Me.TextBox2.Value)
If matricule = "" Then MsgBox "المرجو إدخال رقم التسجيل لحذفه", vbExclamation, "تنبيه": Exit Sub
If Not IsDate(Me.TextBox3.Value) Then MsgBox "المرجو إدخال التاريخ", vbExclamation, "خطأ": Exit Sub
tmps = CDate(Me.TextBox3.Value)
If MsgBox("هل أنت متأكد من حذف هذا التسجيل؟" & vbCrLf & _
"رقم التسجيل: " & matricule & vbCrLf & _
"تاريخ التسجيل: " & Format(tmps, "dd/mm/yyyy"), _
vbYesNo + vbQuestion, "تأكيد الحذف") = vbNo Then Exit Sub
OnRng = WS.Range("B8:C22").Value
supprimé = False
For i = 1 To UBound(OnRng, 1)
If Trim(OnRng(i, 1)) = matricule And IsDate(OnRng(i, 2)) And CDate(OnRng(i, 2)) = tmps Then
OnRng(i, 1) = "": OnRng(i, 2) = "": supprimé = True: Exit For
End If
Next i
If supprimé Then
WS.Range("B8:C22").Value = OnRng
MsgBox "تم حذف التسجيل بنجاح", vbInformation
Else
MsgBox "لم يتم العثور على التسجيل المطلوب", vbExclamation, "غير موجود"
End If
Me.TextBox2.Value = "": Me.TextBox3.Value = ""
End Sub
Castrole v2.xlsm