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

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

قام بنشر (معدل)

 

 

 

Option Explicit

' Error handler subroutine
Sub ErrorHandler(errorNum As Integer, errorDesc As String)
    ' Display error message
    MsgBox "Error: " & errorNum & " - " & errorDesc, vbExclamation
    ' Log the error if needed
    ' LogErrorToFile errorNum, errorDesc
    
    ' Exit the subroutine to stop further execution
    Exit Sub
End Sub

' Subroutine to add a transaction
Sub AddTransaction()
    On Error GoTo ErrorHandler
    
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Transactions")
    
    ' Check if worksheet exists
    If ws Is Nothing Then
        Call ErrorHandler(1004, "Worksheet 'Transactions' not found.")
        Exit Sub
    End If
    
    ' Check for empty fields
    If IsEmpty(ws.Range("TransactionDate").Value) Or _
       IsEmpty(ws.Range("Description").Value) Or _
       IsEmpty(ws.Range("Amount").Value) Or _
       IsEmpty(ws.Range("TransactionType").Value) Then
       
        Call ErrorHandler(1001, "Please enter all data.")
        Exit Sub
    End If
    
    ' Check if amount is numeric and positive
    If Not IsNumeric(ws.Range("Amount").Value) Or ws.Range("Amount").Value <= 0 Then
        Call ErrorHandler(1002, "Please enter a valid positive amount.")
        Exit Sub
    End If
    
    ' Check transaction type
    If ws.Range("TransactionType").Value <> "Sale" And ws.Range("TransactionType").Value <> "Purchase" Then
        Call ErrorHandler(1003, "Please choose transaction type (Sale or Purchase).")
        Exit Sub
    End If
    
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
    
    With ws
        .Cells(lastRow, 1).Value = DateValue(.Range("TransactionDate").Value)
        .Cells(lastRow, 2).Value = .Range("Description").Value
        .Cells(lastRow, 3).Value = .Range("Amount").Value
        .Cells(lastRow, 4).Value = IIf(.Range("TransactionType").Value = "Sale", "Sale", "Purchase")
    End With
    
    MsgBox "Transaction added successfully!", vbInformation
    Exit Sub
    
ErrorHandler:
    Call ErrorHandler(Err.Number, Err.Description)
End Sub

' Subroutine to generate a report
Sub PrintReport(reportType As String)
    On Error GoTo ErrorHandler
    
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Transactions")
    
    ' Check if worksheet exists
    If ws Is Nothing Then
        Call ErrorHandler(1004, "Worksheet 'Transactions' not found.")
        Exit Sub
    End If
    
    ' Check if "ReportDate" cell exists and is not empty
    If IsEmpty(ws.Range("ReportDate").Value) Then
        MsgBox "Please enter a date in the 'ReportDate' cell to generate a report.", vbExclamation
        Exit Sub
    End If
    
    Dim reportWs As Worksheet
    Set reportWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    reportWs.Name = "Report"
    reportWs.Cells.Clear
    
    ' Check if there's data in the "Transactions" worksheet
    If ws.Cells(ws.Rows.Count, 1).End(xlUp).Row = 0 Then
        MsgBox "There are no transactions to report. Please add transactions first.", vbExclamation
        Exit Sub
    End If
    
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
    Dim reportRow As Long
    reportRow = 1
    
    ' Add report headers
    With reportWs
        .Cells(reportRow, 1).Value = "Date"
        .Cells(reportRow, 2).Value = "Description"
        .Cells(reportRow, 3).Value = "Amount"
        .Cells(reportRow, 4).Value = "Type"
    End With
    
    ' Add data to report
    For i = 2 To lastRow
        reportRow = reportRow + 1
        With reportWs
            .Cells(reportRow, 1).Value = ws.Cells(i, 1).Value
            .Cells(reportRow, 2).Value = ws.Cells(i, 2).Value
            .Cells(reportRow, 3).Value = ws.Cells(i, 3).Value
            .Cells(reportRow, 4).Value = ws.Cells(i, 4).Value
        End With
    Next i
    
    MsgBox "Report generated successfully!", vbInformation
    Exit Sub
    
ErrorHandler:
    Call ErrorHandler(Err.Number, Err.Description)
End Sub

 

تم تعديل بواسطه m_127899
قام بنشر

الشيء الذي يبدو غير صحيح في الكود هو كيفية استخدام المعالج الخطأ (Error Handler). تستخدم المعالج في الوظائف `AddTransaction` و `PrintReport` لكنه يدعو دائمًا نفس الدالة `ErrorHandler` بالرقم والوصف المحددين، بغض النظر عن الخطأ الفعلي الذي يحدث.

لتصحيح هذا، يمكنك استخدام `Err.Number` و `Err.Description` للحصول على الخطأ الفعلي الذي حدث وإرساله إلى الدالة `ErrorHandler`. هناك عدة طرق لتحقيق ذلك، لكن إحدى الطرق البسيطة هي استبدال السطر:

```vba
Call ErrorHandler(Err.Number, Err.Description)
```

بالسطر:

```vba
Call ErrorHandler(errorNum, errorDesc)
```

وذلك ليتم نقل الرقم والوصف الفعليين للخطأ الحدث إلى الدالة `ErrorHandler` فيما يخص كل من الدوال `AddTransaction` و `PrintReport`.

هكذا ستتمكن من الحصول على المزيد من المعلومات حول الخطأ الذي يحدث، مما يسهل تحديد وإصلاح المشكلات في الكود.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

زائر
اضف رد علي هذا الموضوع....

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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

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

Important Information