بعد إذن الاستاذ/ هشام
جرب كود الأستاذ/هشام بعد تعديل بسيط
Option Explicit
Sub Transfer()
Dim code As Variant, c As Boolean
Dim tmp(0 To 4) As Boolean, xDate As String
Dim f As Long, i As Long, j As Long
Dim linge As Long, xCode As Boolean, Irow As Range
Dim ColArr As Long, xName As String, n As Variant, val As Variant
Dim lastRow As Long
Dim CrWS As Worksheet: Set CrWS = Sheets("Sheet2")
Dim Data As Worksheet: Set Data = Sheets("Sheet3")
' التحقق من وجود التاريخ
xDate = Format(CrWS.Range("D2").Value, "dd/mm/yyyy")
If xDate = "" Then
MsgBox "المرجوا تحديد التاريخ", vbInformation
Exit Sub
End If
' البحث عن العمود المطابق للتاريخ في الصف 3
With Data
For ColArr = .Columns("E").Column To .Cells(3, .Columns.Count).End(xlToLeft).Column
If Format(.Cells(3, ColArr).Value, "dd/mm/yyyy") = xDate Then
f = ColArr
Exit For
End If
Next ColArr
If f = 0 Then
MsgBox "لم يتم العثور على التاريخ", vbExclamation
Exit Sub
End If
End With
' تحديد آخر صف يحتوي أكواد في العمود C من Sheet2
lastRow = CrWS.Cells(CrWS.Rows.Count, "C").End(xlUp).Row
xCode = False: c = False
' البدء من الصف 11 حتى يشمل أول طالب
For i = 11 To lastRow
code = CrWS.Cells(i, "C").Value
If code <> "" Then
linge = Data.Cells(Data.Rows.Count, "D").End(xlUp).Row
n = Application.Match(code, Data.Range("D6:D" & linge), 0)
If Not IsError(n) Then
xCode = True
' مسح الصف الخاص بالكود الحالي فقط
For ColArr = 0 To 4
Data.Cells(n + 5, f + ColArr).ClearContents
Next ColArr
' نقل القيم
For j = 0 To 4
xName = CrWS.Cells(10, 4 + j).Value
For ColArr = 0 To 4
If Data.Cells(4, f + ColArr).Value = xName Then
val = CrWS.Cells(i, 4 + j).Value
If Not IsEmpty(val) Then
Data.Cells(n + 5, f + ColArr).Value = val
c = True
If Not tmp(j) Then
Data.Cells(5, f + ColArr).Value = CrWS.Cells(11, 4 + j).Value
tmp(j) = True
End If
End If
Exit For
End If
Next ColArr
Next j
End If
End If
Next i
' رسائل النهاية
If Not xCode Then
MsgBox "لم يتم العثور على أي أكواد مطابقة", vbExclamation
ElseIf c Then
MsgBox "تم ترحيل البيانات بنجاح", vbInformation
Else
MsgBox "لا توجد بيانات لترحيلها", vbInformation
End If
End Sub
غياب3.xlsm