This Decision Support System (DSS) project will detect fraudulent transactions using Microsoft Excel Visual Basic for Applications (VBA) by applying rule-based anomaly detection.
We will analyze financial transactions and flag suspicious ones based on:
Create a worksheet named "Transactions" with the following headers in Row 1:
Transaction ID | Date | Time | Account Number | Transaction Amount | Location | Suspicious? |
---|---|---|---|---|---|---|
1001 | 02/08/2025 | 10:30 AM | 453627 | 5000 | New York | |
1002 | 02/08/2025 | 10:32 AM | 453627 | 5100 | Los Angeles | |
1003 | 02/08/2025 | 11:00 AM | 892745 | 250 | Chicago |
➡️ The "Suspicious?" column will be automatically updated by VBA based on predefined fraud rules.
Sub DetectFraud()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim transAmount As Double
Dim prevAmount As Double
Dim accountNum As String
Dim prevAccount As String
Dim transTime As Date
Dim prevTime As Date
Dim transLocation As String
Dim prevLocation As String
Dim timeDiff As Double
' Set worksheet
Set ws = ThisWorkbook.Sheets("Transactions")
' Find the last row with data
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Loop through transactions (starting from row 2)
For i = 2 To lastRow
' Read transaction details
transAmount = ws.Cells(i, 5).Value ' Transaction Amount
accountNum = ws.Cells(i, 4).Value ' Account Number
transTime = ws.Cells(i, 3).Value ' Time
transLocation = ws.Cells(i, 6).Value ' Location
' Compare with previous transaction (if exists)
If i > 2 Then
prevAmount = ws.Cells(i - 1, 5).Value
prevAccount = ws.Cells(i - 1, 4).Value
prevTime = ws.Cells(i - 1, 3).Value
prevLocation = ws.Cells(i - 1, 6).Value
' Time difference in minutes
timeDiff = DateDiff("n", prevTime, transTime)
' Fraud detection rules
If transAmount > 5000 Then
ws.Cells(i, 7).Value = "⚠ High Amount"
ws.Cells(i, 7).Interior.Color = RGB(255, 153, 153) ' Red Highlight
ElseIf prevAccount = accountNum And timeDiff < 5 And prevLocation <> transLocation Then
ws.Cells(i, 7).Value = "⚠ Rapid Location Change"
ws.Cells(i, 7).Interior.Color = RGB(255, 204, 102) ' Orange Highlight
ElseIf prevAccount = accountNum And Abs(transAmount - prevAmount) > 4000 Then
ws.Cells(i, 7).Value = "⚠ Sudden Large Change"
ws.Cells(i, 7).Interior.Color = RGB(255, 204, 102) ' Orange Highlight
Else
ws.Cells(i, 7).Value = "✔ Normal"
ws.Cells(i, 7).Interior.Color = RGB(204, 255, 204) ' Green Highlight
End If
Else
' First transaction is always normal
ws.Cells(i, 7).Value = "✔ Normal"
ws.Cells(i, 7).Interior.Color = RGB(204, 255, 204)
End If
Next i
' Message after completion
MsgBox "Fraud detection completed!", vbInformation, "Fraud Detection"
End Sub
To send an email alert when a fraudulent transaction is detected, modify the script to send an email using Outlook.
Sub SendFraudAlert()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim OutApp As Object
Dim OutMail As Object
Dim emailBody As String
Dim fraudFound As Boolean
' Set worksheet
Set ws = ThisWorkbook.Sheets("Transactions")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Initialize email body
emailBody = "Fraud Alert! The following suspicious transactions were detected:" & vbNewLine & vbNewLine
' Check for fraudulent transactions
fraudFound = False
For i = 2 To lastRow
If ws.Cells(i, 7).Value <> "✔ Normal" Then
emailBody = emailBody & "Transaction ID: " & ws.Cells(i, 1).Value & _
", Amount: $" & ws.Cells(i, 5).Value & _
", Suspicion: " & ws.Cells(i, 7).Value & vbNewLine
fraudFound = True
End If
Next i
' Send email if fraud is found
If fraudFound Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "fraudalerts@bank.com"
.Subject = "🚨 Fraud Alert: Suspicious Transactions Detected"
.Body = emailBody
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
MsgBox "Fraud alert email sent!", vbInformation, "Email Sent"
Else
MsgBox "No fraud detected.", vbInformation, "Fraud Detection"
End If
End Sub
DetectFraud
macro to the button.SendFraudAlert
.
✅ Uses VBA to analyze financial data and detect fraud.
✅ Highlights suspicious transactions using color coding.
✅ Automates fraud alerts via Outlook emails.
✅ Decision Support for Business: Helps banks and businesses reduce fraud losses.
Would you like to extend this with AI-based anomaly detection in Excel? 🚀