اذهب الي المحتوي
أوفيسنا
بحث مخصص من جوجل فى أوفيسنا
Custom Search

الردود الموصى بها

  • 3 months later...
قام بنشر

السلام عليكم
شفت ملفك. القيد بيتكتب في ورقة "القيود" في بلوك لكل قيد، والترحيل المفروض يروح لورقة الشهر (اللي اسمها رقم الشهر) في صف واحد: رقم القيد والتاريخ والبيان، وبعدها المدين والدائن في عمود كل حساب. الكود ده بيعمل كده: بيسألك عن رقم القيد، بيجيب التاريخ ويحدد الشهر، يتأكد إن القيد متزن، وإن كل حساب له عمود في ورقة الشهر، وإن القيد ما اترحّلش قبل كده، وبعدين يكتب الصف. لو في حساب مالوش عمود هيقولك اسمه من غير ما يكتب حاجة. (حطيت أول 4 سطور Const فوق الماكرو خارج أي Sub، خليهم زي ما هم.)

Private Const SRC As String = "القيود"
Private Const FIRST_ACC_COL As Long = 7          ' العمود G أول حساب في ورقة الشهر
Private Const HEADER_ROW As Long = 3             ' صف أسماء الحسابات
Private Const FIRST_DATA_ROW As Long = 6         ' أول صف للقيود في ورقة الشهر

Sub PostEntry()
    ' ترحيل قيد من ورقة "القيود" إلى ورقة الشهر (اسمها رقم الشهر 1..12)
    Dim ws As Worksheet, wm As Worksheet
    Dim entryNo As Long, startRow As Long, r As Long, c As Long, idx As Long
    Dim d As Variant, desc As String, txt As String, lastAcc As String, acc As String, ownName As String
    Dim deb As Double, cre As Double, totD As Double, totC As Double
    Dim lastCol As Long, outRow As Long, missing As String
    Dim accNames(1 To 100) As String, accDisp(1 To 100) As String
    Dim accDeb(1 To 100) As Double, accCre(1 To 100) As Double, accCnt As Long

    Set ws = ThisWorkbook.Worksheets(SRC)
    entryNo = Val(InputBox("رقم القيد المراد ترحيله:"))
    If entryNo = 0 Then Exit Sub
    startRow = FindEntryStart(ws, entryNo)
    If startRow = 0 Then MsgBox "لم أجد القيد رقم " & entryNo, vbExclamation: Exit Sub

    d = ws.Cells(startRow + 1, 3).Value
    If Not IsDate(d) Then MsgBox "تاريخ القيد غير صحيح", vbExclamation: Exit Sub
    d = CDate(d)

    On Error Resume Next
    Set wm = ThisWorkbook.Worksheets(CStr(Month(d)))
    On Error GoTo 0
    If wm Is Nothing Then MsgBox "لا توجد ورقة باسم " & Month(d), vbExclamation: Exit Sub

    ' منع الترحيل مرتين
    If EntryPosted(wm, entryNo) Then
        MsgBox "القيد رقم " & entryNo & " مرحّل من قبل", vbExclamation: Exit Sub
    End If

    lastCol = wm.Cells(HEADER_ROW, wm.Columns.Count).End(xlToLeft).Column

    For r = startRow + 3 To startRow + 40
        If InStr(CStr(ws.Cells(r, 2).Value), ChrW(1573) & ChrW(1593) & ChrW(1583) & ChrW(1575) & ChrW(1583)) > 0 Then Exit For   ' كلمة إعداد = نهاية القيد
        txt = Trim(CStr(ws.Cells(r, 5).Value))
        If txt <> "" Then
            deb = 0: cre = 0
            If IsNumeric(ws.Cells(r, 2).Value) Then deb = Val(ws.Cells(r, 2).Value)
            If IsNumeric(ws.Cells(r, 3).Value) Then cre = Val(ws.Cells(r, 3).Value)
            If IsAccountLine(txt) Then
                lastAcc = CleanAcc(txt)
                acc = lastAcc
                ownName = txt
            Else
                acc = ""
                ownName = txt
                If deb > 0 Or cre > 0 Then
                    ' سطر تحليلي عليه مبلغ: لو له عمود بنفسه في ورقة الشهر (زي اسم مورد) يترحل عليه، وإلا يتبع آخر حساب
                    If FindAccCol(wm, lastCol, CleanAcc(txt)) > 0 Then acc = CleanAcc(txt) Else acc = lastAcc
                End If
                If desc = "" And deb = 0 And cre = 0 And InStr(txt, ChrW(1605) & ChrW(1584) & ChrW(1603) & ChrW(1608) & ChrW(1585)) = 0 Then desc = txt   ' تجاهل سطر "من مذكورين"
            End If
            If (deb > 0 Or cre > 0) And acc <> "" Then
                idx = 0
                For c = 1 To accCnt
                    If accNames(c) = acc Then idx = c: Exit For
                Next c
                If idx = 0 Then
                    accCnt = accCnt + 1
                    accNames(accCnt) = acc
                    accDisp(accCnt) = Trim(ownName)
                    idx = accCnt
                End If
                accDeb(idx) = accDeb(idx) + deb
                accCre(idx) = accCre(idx) + cre
                totD = totD + deb: totC = totC + cre
            End If
        End If
    Next r

    If accCnt = 0 Then MsgBox "لم أجد مبالغ في القيد", vbExclamation: Exit Sub
    If Round(totD, 2) <> Round(totC, 2) Then
        MsgBox "القيد غير متزن: مدين " & totD & " دائن " & totC, vbCritical: Exit Sub
    End If

    ' تأكد أن كل حساب له عمود في ورقة الشهر قبل الكتابة
    For r = 1 To accCnt
        If FindAccCol(wm, lastCol, accNames(r)) = 0 Then missing = missing & vbCrLf & accDisp(r)
    Next r
    If missing <> "" Then
        MsgBox "حسابات ليس لها عمود في ورقة الشهر:" & missing, vbCritical: Exit Sub
    End If

    outRow = FIRST_DATA_ROW
    Do While Trim(CStr(wm.Cells(outRow, 1).Value)) <> ""
        outRow = outRow + 1
    Loop
    wm.Cells(outRow, 1).Value = entryNo
    wm.Cells(outRow, 2).Value = d
    wm.Cells(outRow, 2).NumberFormat = "dd/mm/yyyy"
    wm.Cells(outRow, 3).Value = desc
    For r = 1 To accCnt
        c = FindAccCol(wm, lastCol, accNames(r))
        If accDeb(r) > 0 Then wm.Cells(outRow, c).Value = accDeb(r)
        If accCre(r) > 0 Then wm.Cells(outRow, c + 1).Value = accCre(r)
    Next r
    MsgBox "تم ترحيل القيد " & entryNo & " إلى ورقة الشهر " & Month(d), vbInformation
End Sub

Private Function EntryPosted(wm As Worksheet, ByVal entryNo As Long) As Boolean
    Dim r As Long, lastRow As Long
    lastRow = wm.Cells(wm.Rows.Count, 1).End(xlUp).Row
    For r = FIRST_DATA_ROW To lastRow
        If Trim(CStr(wm.Cells(r, 1).Value)) <> "" And Val(wm.Cells(r, 1).Value) = entryNo Then
            EntryPosted = True: Exit Function
        End If
    Next r
End Function

Private Function FindAccCol(wm As Worksheet, ByVal lastCol As Long, ByVal accName As String) As Long
    Dim c As Long
    For c = FIRST_ACC_COL To lastCol Step 2
        If CleanAcc(CStr(wm.Cells(HEADER_ROW, c).Value)) = accName Then FindAccCol = c: Exit Function
    Next c
End Function

Private Function FindEntryStart(ws As Worksheet, ByVal n As Long) As Long
    Dim r As Long, lastRow As Long, s As String, i As Long, digits As String
    lastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row
    For r = 1 To lastRow
        s = CStr(ws.Cells(r, 2).Value)
        If InStr(s, ChrW(1602) & ChrW(1610) & ChrW(1583)) > 0 And InStr(s, "(") > 0 Then   ' كلمة قيد
            digits = ""
            For i = InStr(s, "(") To Len(s)
                If Mid$(s, i, 1) Like "[0-9]" Then digits = digits & Mid$(s, i, 1)
            Next i
            If digits <> "" Then
                If CLng(digits) = n Then FindEntryStart = r: Exit Function
            End If
        End If
    Next r
End Function

Private Function IsAccountLine(ByVal s As String) As Boolean
    s = Replace(Replace(Trim(s), ChrW(1600), ""), " ", "")
    IsAccountLine = (Left$(s, 2) = ChrW(1581) & "/") Or (Left$(s, 3) = ChrW(1575) & ChrW(1604) & ChrW(1609)) Or (Left$(s, 3) = ChrW(1575) & ChrW(1604) & ChrW(1610))
End Function

Private Function CleanAcc(ByVal s As String) As String
    ' يشيل التطويل والمسافات وبادئات: حـ/  الى  الي  ثم "ال" التعريف
    s = Replace(Replace(Trim(s), ChrW(1600), ""), " ", "")
    If Left$(s, 3) = ChrW(1575) & ChrW(1604) & ChrW(1609) Or Left$(s, 3) = ChrW(1575) & ChrW(1604) & ChrW(1610) Then s = Mid$(s, 4)
    If Left$(s, 2) = ChrW(1581) & "/" Then s = Mid$(s, 3)
    If Left$(s, 2) = ChrW(1575) & ChrW(1604) And Len(s) > 3 Then s = Mid$(s, 3)
    CleanAcc = s
End Function

افتراضاتي من الملف: أسماء الحسابات في الصف 3 من العمود G وكل حساب ياخد عمودين (مدين ودائن)، وأول قيد بيتكتب من الصف 6. لو غيرت التصميم قولي. وجرب الأول على نسخة، لأن أسماء الحسابات بتتقارن بعد ما نشيل "حـ /" و"الى" و"ال" فلو الاسم في القيد مختلف عن الاسم في ورقة الشهر هيقولك.
ولو نفع اختاره أفضل إجابة.

  • Like 1
قام بنشر

انا اسف فى التاخير فى الرد

شكرا لمجهود حضرتك بس لو امكن ممكن حضرتك تضيف الحل فى ملف الاكسيل علشان اقدر افهم علشان انا مش فاهم المفروض اعمل ايه

انشئ حساب جديد او قم بتسجيل دخولك لتتمكن من اضافه تعليق جديد

يجب ان تكون عضوا لدينا لتتمكن من التعليق

انشئ حساب جديد

سجل حسابك الجديد لدينا في الموقع بمنتهي السهوله .

سجل حساب جديد

تسجيل دخول

هل تمتلك حساب بالفعل ؟ سجل دخولك من هنا.

سجل دخولك الان
  • تصفح هذا الموضوع مؤخراً   0 اعضاء متواجدين الان

    • لايوجد اعضاء مسجلون يتصفحون هذه الصفحه
×
×
  • اضف...

Important Information