Inventory Management Using Excel and VBA |
Part 3: VBA Automation, UserForms, and Transaction Processing |
1. Introduction to VBA Automation in Inventory Systems |
After establishing a structured data model in Part 2, the next step is to transform the Excel workbook into an interactive system using VBA. Automation eliminates repetitive manual tasks, enforces data integrity, and provides a user-friendly interface. |
In this part, we will focus on: |
1. Creating VBA modules and procedures |
2. Building UserForms for data entry |
3. Automating transaction recording |
4. Generating unique IDs |
5. Implementing validation and error handling |

|
2. Setting Up the VBA Environment |
Before writing code, the environment must be properly configured. |
2.1 Opening the VBA Editor |
Steps: |
1. Press ALT + F11 |
2. The Visual Basic Editor (VBE) opens |
3. Locate your workbook in the Project Explorer |
2.2 Inserting a Module |
Steps: |
1. Right-click the project |
2. Select Insert Module |
3. A new code module appears |
Modules are used to store general procedures and functions. |
2.3 Inserting a UserForm |
Steps: |
1. Click Insert UserForm |
2. A blank form appears |
3. Use the Toolbox to add controls |

|
3. Designing the Product Entry UserForm |
A UserForm provides a structured way to input product data. |
3.1 Required Controls |
Add the following elements: |
1. Labels (for field names) |
2. TextBoxes (for input) |
3. ComboBoxes (for categories or suppliers) |
4. CommandButtons (Submit, Cancel) |
3.2 Example Control Naming |
Use consistent naming conventions: |
1. txtProductID |
2. txtProductName |
3. cmbCategory |
4. txtCostPrice |
5. btnSave |
3.3 Layout Design Principles |
1. Align controls neatly |
2. Group related fields |
3. Use clear labels |
4. Provide adequate spacing |

|
4. Writing Code for Product Entry |
The goal is to save form data into the Product Sheet. |
4.1 Basic Save Procedure |
Example VBA code: |
```vba |
Private Sub btnSave_Click() |
Dim ws As Worksheet |
Set ws = ThisWorkbook.Sheets('Products') |
Dim nextRow As Long |
nextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1 |
ws.Cells(nextRow, 1).Value = txtProductID.Value |
ws.Cells(nextRow, 2).Value = txtProductName.Value |
ws.Cells(nextRow, 3).Value = cmbCategory.Value |
ws.Cells(nextRow, 4).Value = txtUnit.Value |
ws.Cells(nextRow, 5).Value = txtCostPrice.Value |
ws.Cells(nextRow, 6).Value = txtSellingPrice.Value |
ws.Cells(nextRow, 7).Value = cmbSupplier.Value |
ws.Cells(nextRow, 8).Value = txtReorderLevel.Value |
MsgBox 'Product saved successfully' |
End Sub |
``` |

|
4.2 Explanation of Logic |
1. Identify the Products sheet |
2. Find the next empty row |
3. Write values from form controls |
4. Confirm success with a message |

|
5. Creating Transaction Entry UserForm |
This form handles inventory movements. |
5.1 Required Fields |
1. TransactionID |
2. Date |
3. ProductID |
4. TransactionType |
5. Quantity |
6. UnitPrice |
5.2 Control Types |
1. TextBox for ID and Quantity |
2. ComboBox for ProductID |
3. ComboBox for TransactionType |
4. Date Picker or TextBox for Date |

|
6. Populating ComboBoxes Dynamically |
ComboBoxes should load data automatically. |
6.1 Loading Product List |
Example: |
```vba |
Private Sub UserForm_Initialize() |
Dim ws As Worksheet |
Set ws = ThisWorkbook.Sheets('Products') |
Dim i As Long |
For i = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row |
cmbProductID.AddItem ws.Cells(i, 1).Value |
Next i |
End Sub |
``` |

|
6.2 Loading Transaction Types |
```vba |
cmbTransactionType.AddItem 'Purchase' |
cmbTransactionType.AddItem 'Sale' |
cmbTransactionType.AddItem 'Return In' |
cmbTransactionType.AddItem 'Return Out' |
cmbTransactionType.AddItem 'Adjustment' |
``` |

|
7. Automating Transaction Recording |
Transactions must be recorded and processed. |
7.1 Save Transaction Code |
```vba |
Private Sub btnSubmit_Click() |
Dim ws As Worksheet |
Set ws = ThisWorkbook.Sheets('Transactions') |
Dim nextRow As Long |
nextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1 |
ws.Cells(nextRow, 1).Value = txtTransactionID.Value |
ws.Cells(nextRow, 2).Value = txtDate.Value |
ws.Cells(nextRow, 3).Value = cmbProductID.Value |
ws.Cells(nextRow, 4).Value = cmbTransactionType.Value |
ws.Cells(nextRow, 5).Value = txtQuantity.Value |
ws.Cells(nextRow, 6).Value = txtUnitPrice.Value |
ws.Cells(nextRow, 7).Value = txtQuantity.Value * txtUnitPrice.Value |
MsgBox 'Transaction recorded' |
End Sub |
``` |

|
8. Updating Stock Automatically via VBA |
Instead of relying only on formulas, VBA can update stock. |
8.1 Basic Logic |
1. If Purchase Add stock |
2. If Sale Subtract stock |
8.2 Example Code |
```vba |
Dim stockChange As Double |
If cmbTransactionType.Value = 'Purchase' Then |
stockChange = txtQuantity.Value |
ElseIf cmbTransactionType.Value = 'Sale' Then |
stockChange = -txtQuantity.Value |
End If |
``` |

|
9. Generating Unique IDs Automatically |
Manual ID entry is error-prone. |
9.1 Product ID Generator |
```vba |
Function GenerateProductID() As String |
Dim lastRow As Long |
lastRow = Sheets('Products').Cells(Rows.Count, 1).End(xlUp).Row |
GenerateProductID = 'P' & Format(lastRow, '0000') |
End Function |
``` |
9.2 Transaction ID Generator |
```vba |
Function GenerateTransactionID() As String |
Dim lastRow As Long |
lastRow = Sheets('Transactions').Cells(Rows.Count, 1).End(xlUp).Row |
GenerateTransactionID = 'T' & Format(lastRow, '0000') |
End Function |
``` |

|
10. Input Validation in VBA |
Validation ensures data accuracy. |
10.1 Example Validation Code |
```vba |
If txtProductName.Value = '' Then |
MsgBox 'Product name is required' |
Exit Sub |
End If |
``` |
10.2 Numeric Validation |
```vba |
If Not IsNumeric(txtQuantity.Value) Then |
MsgBox 'Quantity must be numeric' |
Exit Sub |
End If |
``` |

|
11. Preventing Duplicate Product Entries |
Avoid duplicate ProductIDs. |
```vba |
Dim found As Range |
Set found = Sheets('Products').Columns(1).Find(txtProductID.Value) |
If Not found Is Nothing Then |
MsgBox 'Product ID already exists' |
Exit Sub |
End If |
``` |

|
12. Clearing Form After Submission |
Reset fields after saving: |
```vba |
txtProductName.Value = '' |
txtCostPrice.Value = '' |
cmbCategory.Value = '' |
``` |

|
13. Error Handling Implementation |
Prevent crashes using structured error handling. |
```vba |
On Error GoTo ErrorHandler |
' Code here |
Exit Sub |
ErrorHandler: |
MsgBox 'An error occurred: ' & Err.Description |
``` |

|
14. Enhancing User Experience |
Improve usability by: |
1. Auto-filling prices |
2. Highlighting required fields |
3. Using default values |

|
15. Auto-Filling Product Details |
When ProductID is selected: |
```vba |
Private Sub cmbProductID_Change() |
Dim ws As Worksheet |
Set ws = Sheets('Products') |
Dim found As Range |
Set found = ws.Columns(1).Find(cmbProductID.Value) |
If Not found Is Nothing Then |
txtUnitPrice.Value = found.Offset(0, 5).Value |
End If |
End Sub |
``` |

|
16. Creating Navigation Buttons |
Add buttons to open forms: |
```vba |
Sub OpenProductForm() |
frmProduct.Show |
End Sub |
``` |

|
17. Automating Workbook Initialization |
Run code when workbook opens: |
```vba |
Private Sub Workbook_Open() |
MsgBox 'Inventory system loaded' |
End Sub |
``` |

|
18. Protecting Data via VBA |
Restrict editing: |
```vba |
Sheets('Products').Protect Password:='1234' |
``` |

|
19. Testing the Automation |
Test scenarios: |
1. Add new product |
2. Record purchase |
3. Record sale |
4. Verify stock |

|
20. Summary of Part 3 |
In this part, we: |
1. Built UserForms for data entry |
2. Automated product and transaction recording |
3. Implemented validation and error handling |
4. Generated unique IDs |
5. Enhanced usability and workflow |

|
Next: Part 4 Preview |
In Part 4, we will cover: |
1. Advanced stock calculations |
2. Real-time inventory updates |
3. Building dynamic reports |
4. Creating dashboards |
5. Implementing reorder alerts |