0% found this document useful (0 votes)
9 views11 pages

Buffer Stock Management System

Uploaded by

dwi sukmana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

Buffer Stock Management System

Uploaded by

dwi sukmana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

'===================================

' Buffer Stock Management System


'===================================

Option Explicit

' Global variables


Dim wsData As Worksheet
Dim wsCategories As Worksheet
Dim wsSettings As Worksheet

'===================================
' Main Procedures
'===================================

Sub Initialize()
' Set worksheet references
Set wsData = ThisWorkbook.Sheets("Inventory")
Set wsCategories = ThisWorkbook.Sheets("Categories")
Set wsSettings = ThisWorkbook.Sheets("Settings")

' Create data validation for categories dropdown


CreateCategoriesDropdown

' Format inventory table


FormatInventoryTable

' Create conditional formatting for low stock items


CreateLowStockFormatting

MsgBox "System initialized successfully!", vbInformation


End Sub

Sub AddStock()
' Add stock to selected item
Dim selectedRow As Long
Dim quantityToAdd As Long

' Get the selected row from the active cell


selectedRow = Selection.Row

' Check if a valid row is selected


If selectedRow < 5 Or IsEmpty(wsData.Cells(selectedRow, 2)) Then
MsgBox "Please select a valid product row.", vbExclamation
Exit Sub
End If

' Get quantity to add from input field


quantityToAdd = wsData.Range("H2").Value

' Validate input


If quantityToAdd <= 0 Then
MsgBox "Please enter a valid quantity to add.", vbExclamation
Exit Sub
End If

' Add stock


wsData.Cells(selectedRow, 4).Value = wsData.Cells(selectedRow, 4).Value +
quantityToAdd
' Update last modified date
wsData.Cells(selectedRow, 7).Value = Now()

' Reset input field


wsData.Range("H2").Value = ""

MsgBox "Stock updated successfully!", vbInformation


End Sub

Sub SubtractStock()
' Subtract stock from selected item
Dim selectedRow As Long
Dim quantityToSubtract As Long
Dim currentStock As Long

' Get the selected row from the active cell


selectedRow = Selection.Row

' Check if a valid row is selected


If selectedRow < 5 Or IsEmpty(wsData.Cells(selectedRow, 2)) Then
MsgBox "Please select a valid product row.", vbExclamation
Exit Sub
End If

' Get quantity to subtract from input field


quantityToSubtract = wsData.Range("I2").Value

' Get current stock


currentStock = wsData.Cells(selectedRow, 4).Value

' Validate input


If quantityToSubtract <= 0 Then
MsgBox "Please enter a valid quantity to subtract.", vbExclamation
Exit Sub
End If

' Check if there's enough stock


If quantityToSubtract > currentStock Then
MsgBox "Cannot subtract more than current stock.", vbExclamation
Exit Sub
End If

' Subtract stock


wsData.Cells(selectedRow, 4).Value = currentStock - quantityToSubtract

' Update last modified date


wsData.Cells(selectedRow, 7).Value = Now()

' Reset input field


wsData.Range("I2").Value = ""

MsgBox "Stock updated successfully!", vbInformation


End Sub

Sub AddNewItem()
' Add a new item to inventory
Dim newRow As Long
Dim itemName As String
Dim category As String
Dim leadTime As String
Dim minStock As Long

' Get values from input form


itemName = wsData.Range("B2").Value
category = wsData.Range("C2").Value
leadTime = wsData.Range("D2").Value
minStock = wsData.Range("E2").Value

' Validate inputs


If itemName = "" Or category = "" Or leadTime = "" Or minStock <= 0 Then
MsgBox "Please fill in all required fields.", vbExclamation
Exit Sub
End If

' Find the next empty row


newRow = wsData.Cells(wsData.Rows.Count, "B").End(xlUp).Row + 1

' Add the new item


wsData.Cells(newRow, 2).Value = itemName
wsData.Cells(newRow, 3).Value = category
wsData.Cells(newRow, 4).Value = 0 ' Initial stock
wsData.Cells(newRow, 5).Value = leadTime
wsData.Cells(newRow, 6).Value = minStock
wsData.Cells(newRow, 7).Value = Now() ' Last modified date

' Reset input fields


wsData.Range("B2:E2").ClearContents

MsgBox "New item added successfully!", vbInformation


End Sub

Sub SendLowStockEmail()
' Send email for low stock items
Dim objOutlook As Object
Dim objMail As Object
Dim strBody As String
Dim strSubject As String
Dim lastRow As Long
Dim i As Long
Dim lowStockCount As Long
Dim recipientEmail As String

' Get email recipient from settings


recipientEmail = wsSettings.Range("B2").Value

' Validate email


If recipientEmail = "" Then
MsgBox "Please enter a valid email recipient in Settings sheet.",
vbExclamation
Exit Sub
End If

' Find the last row in the inventory


lastRow = wsData.Cells(wsData.Rows.Count, "B").End(xlUp).Row

' Prepare email body


strSubject = "Low Stock Alert - " & Format(Now(), "yyyy-mm-dd")
strBody = "The following items are low on stock:" & vbCrLf & vbCrLf
strBody = strBody & "Item Name | Category | Current Stock | Lead Time | Min
Stock" & vbCrLf
strBody = strBody &
"------------------------------------------------------------" & vbCrLf

' Check for low stock items


lowStockCount = 0
For i = 5 To lastRow
If wsData.Cells(i, 4).Value <= 2 Then
strBody = strBody & wsData.Cells(i, 2).Value & " | " & _
wsData.Cells(i, 3).Value & " | " & _
wsData.Cells(i, 4).Value & " | " & _
wsData.Cells(i, 5).Value & " | " & _
wsData.Cells(i, 6).Value & vbCrLf
lowStockCount = lowStockCount + 1
End If
Next i

' Exit if no low stock items


If lowStockCount = 0 Then
MsgBox "No low stock items to report.", vbInformation
Exit Sub
End If

' Add priority info for longest lead time items


strBody = strBody & vbCrLf & "Critical Items (Longest Lead Time):" & vbCrLf
strBody = strBody & GetCriticalItems()

' Send email using Google authentication


On Error Resume Next
Set objOutlook = CreateObject("Outlook.Application")

If Err.Number <> 0 Then


MsgBox "Outlook is not installed. Please set up Outlook to use email
functionality.", vbExclamation
Exit Sub
End If

On Error GoTo 0

Set objMail = objOutlook.CreateItem(0)

With objMail
.To = recipientEmail
.Subject = strSubject
.Body = strBody
.Display ' For testing - change to .Send for automatic sending
End With

MsgBox "Low stock email has been created!", vbInformation


End Sub

Sub ScheduleWeeklyEmail()
' Set up a weekly scheduled task to send emails
' Note: This requires Windows Task Scheduler to be set up separately

MsgBox "To schedule weekly emails, please create a Windows Task Scheduler task
that runs this Excel file with the /e switch and the macro name
'SendLowStockEmail'." & vbCrLf & _
"Example command: Excel.exe ""C:\path\to\this\file.xlsm"" /e
SendLowStockEmail", vbInformation
End Sub

'===================================
' Helper Functions
'===================================

Function GetCriticalItems() As String


' Get items that are almost out of stock with longest lead time
Dim lastRow As Long
Dim i As Long
Dim criticalItems As String
Dim longLeadTimeItems As Collection

Set longLeadTimeItems = New Collection

' Find the last row in the inventory


lastRow = wsData.Cells(wsData.Rows.Count, "B").End(xlUp).Row

' First identify items with "More than 1 month" lead time
For i = 5 To lastRow
If wsData.Cells(i, 4).Value <= wsData.Cells(i, 6).Value And _
wsData.Cells(i, 5).Value = "More than 1 month" Then
longLeadTimeItems.Add i
End If
Next i

' If none found, check for "1 month" items


If longLeadTimeItems.Count = 0 Then
For i = 5 To lastRow
If wsData.Cells(i, 4).Value <= wsData.Cells(i, 6).Value And _
wsData.Cells(i, 5).Value = "1 month" Then
longLeadTimeItems.Add i
End If
Next i
End If

' If still none, check for "2 weeks" items


If longLeadTimeItems.Count = 0 Then
For i = 5 To lastRow
If wsData.Cells(i, 4).Value <= wsData.Cells(i, 6).Value And _
wsData.Cells(i, 5).Value = "2 weeks" Then
longLeadTimeItems.Add i
End If
Next i
End If

' If still none, check for "1 week" items


If longLeadTimeItems.Count = 0 Then
For i = 5 To lastRow
If wsData.Cells(i, 4).Value <= wsData.Cells(i, 6).Value And _
wsData.Cells(i, 5).Value = "1 week" Then
longLeadTimeItems.Add i
End If
Next i
End If
' Prepare the critical items string
criticalItems = ""

For i = 1 To longLeadTimeItems.Count
Dim row As Long
row = longLeadTimeItems(i)

criticalItems = criticalItems & wsData.Cells(row, 2).Value & " | " & _


wsData.Cells(row, 3).Value & " | " & _
wsData.Cells(row, 4).Value & " | " & _
wsData.Cells(row, 5).Value & vbCrLf
Next i

If criticalItems = "" Then


criticalItems = "No critical items found."
End If

GetCriticalItems = criticalItems
End Function

Sub CreateCategoriesDropdown()
' Create data validation for categories dropdown
Dim categoriesRange As String
Dim lastCatRow As Long

' Find the last row in categories sheet


lastCatRow = wsCategories.Cells(wsCategories.Rows.Count, "A").End(xlUp).Row

' Set the validation range


categoriesRange = "Categories!$A$2:$A$" & lastCatRow

' Apply validation to the category input cell


With wsData.Range("C2").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & categoriesRange
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Invalid Category"
.InputMessage = ""
.ErrorMessage = "Please select a category from the list."
.ShowInput = True
.ShowError = True
End With

' Apply validation to the category column


With wsData.Range("C5:C1000").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & categoriesRange
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Invalid Category"
.InputMessage = ""
.ErrorMessage = "Please select a category from the list."
.ShowInput = True
.ShowError = True
End With
End Sub

Sub CreateLeadTimeDropdown()
' Create data validation for lead time dropdown
Dim leadTimes As String

' Set the validation values


leadTimes = "1 week,2 weeks,1 month,More than 1 month"

' Apply validation to the lead time input cell


With wsData.Range("D2").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=leadTimes
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Invalid Lead Time"
.InputMessage = ""
.ErrorMessage = "Please select a lead time from the list."
.ShowInput = True
.ShowError = True
End With

' Apply validation to the lead time column


With wsData.Range("E5:E1000").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=leadTimes
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Invalid Lead Time"
.InputMessage = ""
.ErrorMessage = "Please select a lead time from the list."
.ShowInput = True
.ShowError = True
End With
End Sub

Sub FormatInventoryTable()
' Format the inventory table
With wsData.Range("A4:G4")
.Value = Array("ID", "Item Name", "Category", "Current Stock", "Lead Time",
"Min Stock", "Last Modified")
.Font.Bold = True
.Interior.Color = RGB(200, 200, 200)
End With

' Set column widths


wsData.Columns("A").ColumnWidth = 5
wsData.Columns("B").ColumnWidth = 25
wsData.Columns("C").ColumnWidth = 15
wsData.Columns("D").ColumnWidth = 15
wsData.Columns("E").ColumnWidth = 15
wsData.Columns("F").ColumnWidth = 10
wsData.Columns("G").ColumnWidth = 20
' Create input form area
wsData.Range("A1").Value = "Add New Item:"
wsData.Range("A1").Font.Bold = True
wsData.Range("A2:E2").Interior.Color = RGB(230, 230, 230)

wsData.Range("G1").Value = "Update Stock:"


wsData.Range("G1").Font.Bold = True
wsData.Range("H1").Value = "Add"
wsData.Range("I1").Value = "Subtract"
wsData.Range("H1:I1").Font.Bold = True
wsData.Range("H2:I2").Interior.Color = RGB(230, 230, 230)

' Add buttons


AddFormButtons
End Sub

Sub AddFormButtons()
' Add control buttons to the sheet
Dim btn As Button

' Remove any existing buttons


On Error Resume Next
wsData.Buttons.Delete
On Error GoTo 0

' Add New Item button


Set btn = wsData.Buttons.Add(100, 30, 80, 25)
With btn
.Caption = "Add Item"
.Name = "btnAddItem"
.OnAction = "AddNewItem"
End With

' Add Stock button


Set btn = wsData.Buttons.Add(320, 30, 80, 25)
With btn
.Caption = "Add Stock"
.Name = "btnAddStock"
.OnAction = "AddStock"
End With

' Subtract Stock button


Set btn = wsData.Buttons.Add(410, 30, 80, 25)
With btn
.Caption = "Subtract"
.Name = "btnSubtractStock"
.OnAction = "SubtractStock"
End With

' Send Email button


Set btn = wsData.Buttons.Add(500, 30, 100, 25)
With btn
.Caption = "Send Low Stock Email"
.Name = "btnSendEmail"
.OnAction = "SendLowStockEmail"
End With
End Sub

Sub CreateLowStockFormatting()
' Create conditional formatting for low stock items
Dim inventoryRange As Range
Dim lastRow As Long

' Find the last row


lastRow = wsData.Cells(wsData.Rows.Count, "B").End(xlUp).Row
If lastRow < 5 Then lastRow = 5

' Set the range


Set inventoryRange = wsData.Range("D5:D" & lastRow)

' Clear existing conditional formatting


inventoryRange.FormatConditions.Delete

' Add conditional formatting rule for low stock


With inventoryRange.FormatConditions.Add(Type:=xlCellValue,
Operator:=xlLessEqual, Formula1:="2")
.Interior.Color = RGB(255, 200, 200)
.StopIfTrue = False
End With

' Add another rule for stock below minimum


With inventoryRange.FormatConditions.Add(Type:=xlExpression,
Formula1:="=D5<F5")
.Interior.Color = RGB(255, 150, 150)
.Font.Bold = True
.StopIfTrue = False
End With
End Sub

Sub SetupWorkbook()
' Create the initial workbook structure

' Check if worksheets exist, create if not


On Error Resume Next
Set wsData = ThisWorkbook.Sheets("Inventory")
If wsData Is Nothing Then

ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name
= "Inventory"
Set wsData = ThisWorkbook.Sheets("Inventory")
End If

Set wsCategories = ThisWorkbook.Sheets("Categories")


If wsCategories Is Nothing Then

ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name
= "Categories"
Set wsCategories = ThisWorkbook.Sheets("Categories")
End If

Set wsSettings = ThisWorkbook.Sheets("Settings")


If wsSettings Is Nothing Then

ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name
= "Settings"
Set wsSettings = ThisWorkbook.Sheets("Settings")
End If
On Error GoTo 0
' Setup Categories worksheet
wsCategories.Range("A1").Value = "Categories"
wsCategories.Range("A1").Font.Bold = True
wsCategories.Range("A2").Value = "Electronics"
wsCategories.Range("A3").Value = "Office Supplies"
wsCategories.Range("A4").Value = "Furniture"
wsCategories.Range("A5").Value = "Food & Beverage"
wsCategories.Range("A1").EntireColumn.AutoFit

' Setup Settings worksheet


wsSettings.Range("A1").Value = "Email Settings"
wsSettings.Range("A1").Font.Bold = True
wsSettings.Range("A2").Value = "Email Recipient:"
wsSettings.Range("B2").Value = ""
wsSettings.Range("A3").Value = "Schedule:"
wsSettings.Range("B3").Value = "Weekly"
wsSettings.Range("A1:B1").EntireColumn.AutoFit

' Initialize the inventory worksheet


FormatInventoryTable
CreateCategoriesDropdown
CreateLeadTimeDropdown

MsgBox "Workbook setup complete!", vbInformation


End Sub

Sub FilterByLeadTime()
' Filter inventory by lead time
Dim leadTime As String
leadTime = wsData.Range("K2").Value

' Ensure data area is selected


wsData.Range("A4").Select

' Make sure AutoFilter is applied


If Not wsData.AutoFilterMode Then
wsData.Range("A4:G4").AutoFilter
End If

' Apply filter


If leadTime = "All" Then
wsData.AutoFilter.ShowAllData
Else
wsData.Range("A4:G4").AutoFilter Field:=5, Criteria1:=leadTime
End If
End Sub

Sub ShowCriticalItems()
' Filter to show items that are almost out of stock and have longest lead time

' Ensure data area is selected


wsData.Range("A4").Select

' Make sure AutoFilter is applied


If Not wsData.AutoFilterMode Then
wsData.Range("A4:G4").AutoFilter
End If
' First clear any existing filters
wsData.AutoFilter.ShowAllData

' Apply filter for stock level <= minimum stock


wsData.Range("A4:G4").AutoFilter Field:=4, Criteria1:="<=" &
wsData.Range("F5").Address(False, False), Operator:=xlAnd

' Apply second filter for longest lead time


wsData.Range("A4:G4").AutoFilter Field:=5, Criteria1:="More than 1 month"
End Sub

'===================================
' Workbook Events
'===================================

Private Sub Workbook_Open()


' Initialize on workbook open
On Error Resume Next
SetupWorkbook
Initialize
On Error GoTo 0
End Sub

You might also like