You are on page 1of 23

Private Function IsFieldValid(ByVal fieldValue As Variant) As Boolean

' Check if the field value is not empty


If Not IsEmpty(fieldValue) Then
IsFieldValid = True
Else
IsFieldValid = False
End If
End Function

Private Sub EmployeeIDTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


' Allow only numeric input in the EmployeeIDTextBox
If KeyAscii >= 48 And KeyAscii <= 57 Then
' Valid numeric character, allow input
ElseIf KeyAscii = 8 Then
' Backspace, allow input
Else
' Block the input if it's not a numeric character or Backspace
KeyAscii = 0
End If
End Sub
Private Sub EmployeeIDTextBox_Change()
' Remove leading zeros automatically
EmployeeIDTextBox.Text = Val(EmployeeIDTextBox.Text)
End Sub

Private Sub EmployeeIDTextBox_AfterUpdate()


Dim wsCurrent As Worksheet
Dim wsOldMaster As Worksheet
Dim empID As String
Dim empName As String
Dim lastRow As Long
Dim i As Long
Dim position As String
Dim location As String
Dim dateOfJoining As Variant

' Specify the worksheets where employee data is located


Set wsCurrent = ThisWorkbook.Worksheets("Current Employee Data")
Set wsOldMaster = ThisWorkbook.Worksheets("Old Master Data")

' Get the entered Employee ID from the TextBox


empID = Trim(Me.EmployeeIDTextBox.value) ' Remove leading/trailing spaces

' Initialize empName to "Employee ID not found"


empName = "Employee ID not found"

' Search for the Employee ID in "Current Employee Data"


lastRow = wsCurrent.Cells(wsCurrent.Rows.Count, "A").End(xlUp).row
For i = 2 To lastRow
If wsCurrent.Cells(i, 1).value = empID Then
empName = wsCurrent.Cells(i, 2).value
Exit For
End If
Next i

' If not found in "Current Employee Data," search in "Old Master Data"
If empName = "Employee ID not found" Then
lastRow = wsOldMaster.Cells(wsOldMaster.Rows.Count, "B").End(xlUp).row
For i = 2 To lastRow
If wsOldMaster.Cells(i, 2).value = empID Then
empName = wsOldMaster.Cells(i, 5).value
Exit For
End If
Next i
End If

' Display the employee name in the EmployeeNameLabel (Label control)


Me.EmployeeNameLabel.Caption = empName

' Initialize position to "NO DATA AVAILABLE"


position = "NO DATA AVAILABLE"

' Search for the Employee ID in "Current Employee Data"


lastRow = wsCurrent.Cells(wsCurrent.Rows.Count, "A").End(xlUp).row
For i = 2 To lastRow
If wsCurrent.Cells(i, 1).value = empID Then
position = wsCurrent.Cells(i, 9).value ' Assuming position is in Column I
Exit For
End If
Next i

' If not found in "Current Employee Data," search in "Old Master Data"
If position = "NO DATA AVAILABLE" Then
lastRow = wsOldMaster.Cells(wsOldMaster.Rows.Count, "B").End(xlUp).row
For i = 2 To lastRow
If wsOldMaster.Cells(i, 2).value = empID Then
position = wsOldMaster.Cells(i, 12).value ' Assuming position is in Column L
Exit For
End If
Next i
End If

' Display the employee position in the PositionLabel (Label control)


Me.PositionLabel.Caption = position

' Initialize location to "NO DATA AVAILABLE"


location = "NO DATA AVAILABLE"

' Search for the Employee ID in "Current Employee Data"


lastRow = wsCurrent.Cells(wsCurrent.Rows.Count, "A").End(xlUp).row
For i = 2 To lastRow
If wsCurrent.Cells(i, 1).value = empID Then
location = wsCurrent.Cells(i, 8).value ' Assuming location is in Column H of "EmpProject"
Exit For
End If
Next i

' If not found in "Current Employee Data," search in "Old Master Data"
If location = "NO DATA AVAILABLE" Then
lastRow = wsOldMaster.Cells(wsOldMaster.Rows.Count, "B").End(xlUp).row
For i = 2 To lastRow
If wsOldMaster.Cells(i, 2).value = empID Then
location = wsOldMaster.Cells(i, 11).value ' Assuming location is in Column K of "EmpProject"
Exit For
End If
Next i
End If

' Display the employee location in the LocationLabel (Label control)


Me.LocationLabel.Caption = location

' Initialize dateOfJoining to "Date not found"


dateOfJoining = "Date not found"

' Search for the Employee ID in "Current Employee Data"


lastRow = wsCurrent.Cells(wsCurrent.Rows.Count, 1).End(xlUp).row
For i = 2 To lastRow
If wsCurrent.Cells(i, 1).value = empID Then
dateOfJoining = wsCurrent.Cells(i, 5).value ' Assuming Date of Joining is in column E
Exit For
End If
Next i

' If not found in "Current Employee Data," search in "Old Master Data"
If dateOfJoining = "Date not found" Then
lastRow = wsOldMaster.Cells(wsOldMaster.Rows.Count, 2).End(xlUp).row
For i = 2 To lastRow
If wsOldMaster.Cells(i, 2).value = empID Then
dateOfJoining = wsOldMaster.Cells(i, 8).value ' Assuming Date of Joining is in column H
Exit For
End If
Next i
End If

' Display the date of joining in the DateOfJoiningLabel (Label control)


Me.DateOfJoiningLabel.Caption = dateOfJoining

' Clear existing items in the ListView


Me.DataShowListView.ListItems.Clear

' Check if an employee ID is entered


empID = Trim(Me.EmployeeIDTextBox.value) ' Remove leading/trailing spaces

If empID <> "" Then


' Employee ID is entered, so populate the ListView with data
PopulateListViewWithData empID
End If

' Dim empID As String


' Dim wsCurrent As Worksheet
' Dim wsOldMaster As Worksheet
Dim empTickets As Variant
' Dim lastRow As Long
' Dim i As Long
' Get the entered Employee ID from the TextBox
empID = Trim(Me.EmployeeIDTextBox.value) ' Remove leading/trailing spaces

' Specify the worksheets where employee data is located


Set wsCurrent = ThisWorkbook.Worksheets("Current Employee Data")
Set wsOldMaster = ThisWorkbook.Worksheets("Old Master Data")

' Search for the employee ID in "Current Employee Data" in column A


lastRow = wsCurrent.Cells(wsCurrent.Rows.Count, "A").End(xlUp).row
For i = 2 To lastRow
If wsCurrent.Cells(i, 1).value = empID Then
empTickets = wsCurrent.Cells(i, 16).value
Exit For
End If
Next i

If IsEmpty(empTickets) Then
' If not found in "Current Employee Data," search in "Old Master Data" in column B
lastRow = wsOldMaster.Cells(wsOldMaster.Rows.Count, "B").End(xlUp).row
For i = 2 To lastRow
If wsOldMaster.Cells(i, 2).value = empID Then
empTickets = wsOldMaster.Cells(i, 18).value
Exit For
End If
Next i
End If

If IsEmpty(empTickets) Then
' Employee ID not found in either sheet, display a message
Me.FamilyTicketLabel.Caption = "Employee ID not found"
ElseIf empTickets = 1 Then
' Employee is not entitled to a family ticket
Me.FamilyTicketLabel.Caption = "Not entitled for Family Ticket"
ElseIf empTickets >= 3 And empTickets <= 4 Then
' Employee is entitled to 3 or 4 family tickets
Me.FamilyTicketLabel.Caption = empTickets
Else
' Employee is entitled to some other number of tickets
Me.FamilyTicketLabel.Caption = "Other entitlement (" & empTickets & ")"
End If
End Sub
Private Sub LeaveStartDateTextBox_AfterUpdate()
Dim strDate As String
Dim validDate As Date
Dim char As String
Dim i As Integer
Dim validChars As String
UpdateTotalDays

' Define the valid characters (numbers, hyphens, and slashes)


validChars = "0123456789-/"

' Get the entered date as a string


strDate = Me.LeaveStartDateTextBox.value

' Check if the entered date is empty or contains hint text


If Trim(strDate) = "" Or strDate = "DD/MM/YYYY" Then
' Clear the TextBox if it's empty or contains hint text
Me.LeaveStartDateTextBox.value = ""
Else
' Initialize a flag to check for invalid characters
Dim isValid As Boolean
isValid = True
' Loop through each character in the entered text
For i = 1 To Len(strDate)
char = Mid(strDate, i, 1)
' Check if the character is a valid character
If InStr(validChars, char) = 0 Then
' If not, set the flag to indicate an invalid character
isValid = False
Exit For
End If
Next i

' If all characters are valid, proceed with date validation


If isValid Then
' Attempt to convert the entered text to a date
On Error Resume Next
validDate = DateValue(strDate)
On Error GoTo 0

' Check if the conversion was successful and the date is valid
If IsDate(validDate) Then
' Format the date as DD/MMM/YYYY and update the TextBox
Me.LeaveStartDateTextBox.value = Format(validDate, "dd/mmm/yyyy")
Else
' Clear the TextBox and show a message if the date is invalid
Me.LeaveStartDateTextBox.value = ""
MsgBox "Invalid date format. Please use DD/MM/YYYY format.", vbExclamation, "Date Entry
Error"
End If
Else
' Clear the TextBox and show a message for invalid characters
Me.LeaveStartDateTextBox.value = ""
MsgBox "Invalid characters. Please use only numbers, hyphens, and slashes.", vbExclamation,
"Date Entry Error"
End If
End If
End Sub

Private Sub LeaveEndDateTextBox_Enter()


' Check if the TextBox contains the hint text, and if so, clear it
If Me.LeaveEndDateTextBox.value = "DD/MM/YYYY" Then
Me.LeaveEndDateTextBox.value = ""
Me.LeaveEndDateTextBox.ForeColor = RGB(0, 0, 0) ' Set text color to black
End If
End Sub

Private Sub LeaveEndDateTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)


' Check if the TextBox is empty, and if so, display the hint text
If Trim(Me.LeaveEndDateTextBox.value) = "" Then
Me.LeaveEndDateTextBox.value = "DD/MM/YYYY"
Me.LeaveEndDateTextBox.ForeColor = RGB(192, 192, 192) ' Set text color to gray
End If
End Sub

Private Sub LeaveEndDateTextBox_AfterUpdate()


Dim strDate As String
Dim validDate As Date
Dim char As String
Dim i As Integer
Dim validChars As String
UpdateTotalDays

' Define the valid characters (numbers, hyphens, and slashes)


validChars = "0123456789-/"

' Get the entered date as a string


strDate = Me.LeaveEndDateTextBox.value

' Check if the entered date is empty or contains hint text


If Trim(strDate) = "" Or strDate = "DD/MM/YYYY" Then
' Clear the TextBox if it's empty or contains hint text
Me.LeaveEndDateTextBox.value = ""
Else
' Initialize a flag to check for invalid characters
Dim isValid As Boolean
isValid = True

' Loop through each character in the entered text


For i = 1 To Len(strDate)
char = Mid(strDate, i, 1)
' Check if the character is a valid character
If InStr(validChars, char) = 0 Then
' If not, set the flag to indicate an invalid character
isValid = False
Exit For
End If
Next i

' If all characters are valid, proceed with date validation


If isValid Then
' Attempt to convert the entered text to a date
On Error Resume Next
validDate = DateValue(strDate)
On Error GoTo 0

' Check if the conversion was successful and the date is valid
If IsDate(validDate) Then
' Format the date as DD/MMM/YYYY and update the TextBox
Me.LeaveEndDateTextBox.value = Format(validDate, "dd/mmm/yyyy")
Else
' Clear the TextBox and show a message if the date is invalid
Me.LeaveEndDateTextBox.value = ""
MsgBox "Invalid date format. Please use DD/MM/YYYY format.", vbExclamation, "Date Entry
Error"
End If
Else
' Clear the TextBox and show a message for invalid characters
Me.LeaveEndDateTextBox.value = ""
MsgBox "Invalid characters. Please use only numbers, hyphens, and slashes.", vbExclamation,
"Date Entry Error"
End If
End If
End Sub

Private Sub DepartureDateTextBox_Enter()


' Check if the TextBox contains the hint text, and if so, clear it
If Me.DepartureDateTextBox.value = "DD/MM/YYYY" Then
Me.DepartureDateTextBox.value = ""
Me.DepartureDateTextBox.ForeColor = RGB(0, 0, 0) ' Set text color to black
End If
End Sub

Private Sub DepartureDateTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)


' Check if the TextBox is empty, and if so, display the hint text
If Trim(Me.DepartureDateTextBox.value) = "" Then
Me.DepartureDateTextBox.value = "DD/MM/YYYY"
Me.DepartureDateTextBox.ForeColor = RGB(192, 192, 192) ' Set text color to gray
End If
End Sub

Private Sub DepartureDateTextBox_AfterUpdate()


Dim strDate As String
Dim validDate As Date
Dim char As String
Dim i As Integer
Dim validChars As String

' Define the valid characters (numbers, hyphens, and slashes)


validChars = "0123456789-/"

' Get the entered date as a string


strDate = Me.DepartureDateTextBox.value

' Check if the entered date is empty or contains hint text


If Trim(strDate) = "" Or strDate = "DD/MM/YYYY" Then
' Clear the TextBox if it's empty or contains hint text
Me.DepartureDateTextBox.value = ""
Else
' Initialize a flag to check for invalid characters
Dim isValid As Boolean
isValid = True

' Loop through each character in the entered text


For i = 1 To Len(strDate)
char = Mid(strDate, i, 1)
' Check if the character is a valid character
If InStr(validChars, char) = 0 Then
' If not, set the flag to indicate an invalid character
isValid = False
Exit For
End If
Next i

' If all characters are valid, proceed with date validation


If isValid Then
' Attempt to convert the entered text to a date
On Error Resume Next
validDate = DateValue(strDate)
On Error GoTo 0

' Check if the conversion was successful and the date is valid
If IsDate(validDate) Then
' Format the date as DD/MMM/YYYY and update the TextBox
Me.DepartureDateTextBox.value = Format(validDate, "dd/mmm/yyyy")
Else
' Clear the TextBox and show a message if the date is invalid
Me.DepartureDateTextBox.value = ""
MsgBox "Invalid date format. Please use DD/MM/YYYY format.", vbExclamation, "Date Entry
Error"
End If
Else
' Clear the TextBox and show a message for invalid characters
Me.DepartureDateTextBox.value = ""
MsgBox "Invalid characters. Please use only numbers, hyphens, and slashes.", vbExclamation,
"Date Entry Error"
End If
End If
End Sub
Private Sub RequestDateTextBox_Enter()
' Check if the TextBox contains the hint text, and if so, clear it
If Me.RequestDateTextBox.value = "DD/MM/YYYY" Then
Me.RequestDateTextBox.value = ""
Me.RequestDateTextBox.ForeColor = RGB(0, 0, 0) ' Set text color to black
End If
End Sub

Private Sub RequestDateTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)


' Check if the TextBox is empty, and if so, display the hint text
If Trim(Me.RequestDateTextBox.value) = "" Then
Me.RequestDateTextBox.value = "DD/MM/YYYY"
Me.RequestDateTextBox.ForeColor = RGB(192, 192, 192) ' Set text color to gray
End If
End Sub

Private Sub RequestDateTextBox_AfterUpdate()


Dim strDate As String
Dim validDate As Date
Dim char As String
Dim i As Integer
Dim validChars As String

' Define the valid characters (numbers, hyphens, and slashes)


validChars = "0123456789-/"

' Get the entered date as a string


strDate = Me.RequestDateTextBox.value

' Check if the entered date is empty or contains hint text


If Trim(strDate) = "" Or strDate = "DD/MM/YYYY" Then
' Clear the TextBox if it's empty or contains hint text
Me.RequestDateTextBox.value = ""
Else
' Initialize a flag to check for invalid characters
Dim isValid As Boolean
isValid = True

' Loop through each character in the entered text


For i = 1 To Len(strDate)
char = Mid(strDate, i, 1)
' Check if the character is a valid character
If InStr(validChars, char) = 0 Then
' If not, set the flag to indicate an invalid character
isValid = False
Exit For
End If
Next i

' If all characters are valid, proceed with date validation


If isValid Then
' Attempt to convert the entered text to a date
On Error Resume Next
validDate = DateValue(strDate)
On Error GoTo 0

' Check if the conversion was successful and the date is valid
If IsDate(validDate) Then
' Format the date as DD/MMM/YYYY and update the TextBox
Me.RequestDateTextBox.value = Format(validDate, "dd/mmm/yyyy")
Else
' Clear the TextBox and show a message if the date is invalid
Me.RequestDateTextBox.value = ""
MsgBox "Invalid date format. Please use DD/MM/YYYY format.", vbExclamation, "Date Entry
Error"
End If
Else
' Clear the TextBox and show a message for invalid characters
Me.RequestDateTextBox.value = ""
MsgBox "Invalid characters. Please use only numbers, hyphens, and slashes.", vbExclamation,
"Date Entry Error"
End If
End If
End Sub

Private Sub JoiningDateTextBox_Enter()


' Check if the TextBox contains the hint text, and if so, clear it
If Me.JoiningDateTextBox.value = "DD/MM/YYYY" Then
Me.JoiningDateTextBox.value = ""
Me.JoiningDateTextBox.ForeColor = RGB(0, 0, 0) ' Set text color to black
End If
End Sub

Private Sub JoiningDateTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)


' Check if the TextBox is empty, and if so, display the hint text
If Trim(Me.JoiningDateTextBox.value) = "" Then
Me.JoiningDateTextBox.value = "DD/MM/YYYY"
Me.JoiningDateTextBox.ForeColor = RGB(192, 192, 192) ' Set text color to gray
End If
End Sub

Private Sub JoiningDateTextBox_AfterUpdate()


Dim strDate As String
Dim validDate As Date
Dim char As String
Dim i As Integer
Dim validChars As String

' Define the valid characters (numbers, hyphens, and slashes)


validChars = "0123456789-/"

' Get the entered date as a string


strDate = Me.JoiningDateTextBox.value

' Check if the entered date is empty or contains hint text


If Trim(strDate) = "" Or strDate = "DD/MM/YYYY" Then
' Clear the TextBox if it's empty or contains hint text
Me.JoiningDateTextBox.value = ""
Else
' Initialize a flag to check for invalid characters
Dim isValid As Boolean
isValid = True

' Loop through each character in the entered text


For i = 1 To Len(strDate)
char = Mid(strDate, i, 1)
' Check if the character is a valid character
If InStr(validChars, char) = 0 Then
' If not, set the flag to indicate an invalid character
isValid = False
Exit For
End If
Next i

' If all characters are valid, proceed with date validation


If isValid Then
' Attempt to convert the entered text to a date
On Error Resume Next
validDate = DateValue(strDate)
On Error GoTo 0

' Check if the conversion was successful and the date is valid
If IsDate(validDate) Then
' Format the date as DD/MMM/YYYY and update the TextBox
Me.JoiningDateTextBox.value = Format(validDate, "dd/mmm/yyyy")
Else
' Clear the TextBox and show a message if the date is invalid
Me.JoiningDateTextBox.value = ""
MsgBox "Invalid date format. Please use DD/MM/YYYY format.", vbExclamation, "Date Entry
Error"
End If
Else
' Clear the TextBox and show a message for invalid characters
Me.JoiningDateTextBox.value = ""
MsgBox "Invalid characters. Please use only numbers, hyphens, and slashes.", vbExclamation,
"Date Entry Error"
End If
End If
End Sub

Private Sub UserForm_Initialize()


' Initialize the LeaveTypeComboBox with data from the "Setting" worksheet
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Setting")

' Find the last row in column A of the "Setting" worksheet


Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).row

' Define the range of values in column A (from A2 to the last used row)
Dim rangeValues As Range
Set rangeValues = ws.Range("A2:A" & lastRow)

' Populate the ComboBox with values from the range


LeaveTypeComboBox.List = rangeValues.value

' Set the ComboBox's Style property to "2 - fmStyleDropDownList"


LeaveTypeComboBox.Style = fmStyleDropDownList

' Initialize the ListView control with columns


InitializeListViewColumns
' Initialize the TicketUtilizationComboBox with data from the "Setting" worksheet
Set ws = ThisWorkbook.Worksheets("Setting")

' Find the last row in column C of the "Setting" worksheet


lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).row

' Define the range of values in column C (from C2 to the last used row)
Set rangeValues = ws.Range("C2:C" & lastRow)

' Populate the ComboBox with values from the range


TicketUtilizationComboBox.List = rangeValues.value

' Set the ComboBox's Style property to "2 - fmStyleDropDownList"


TicketUtilizationComboBox.Style = fmStyleDropDownList

' Initialize the ListView control with columns


InitializeListViewColumns

' Initialize the FamilyTicketUtlzComboBox with data from the "Setting" worksheet
Dim wsSetting As Worksheet
Set wsSetting = ThisWorkbook.Worksheets("Setting")

Dim lastRowSetting As Long


lastRowSetting = wsSetting.Cells(wsSetting.Rows.Count, "E").End(xlUp).row

Dim rangeValuesSetting As Range


Set rangeValuesSetting = wsSetting.Range("E2:E" & lastRowSetting)

' Populate the ComboBox with values from the range


FamilyTicketUtlzComboBox.List = rangeValuesSetting.value

' Set the ComboBox's Style property to "2 - fmStyleDropDownList"


FamilyTicketUtlzComboBox.Style = fmStyleDropDownList

' Initialize the ListView control with columns


InitializeListViewColumns

End Sub
Private Sub UpdateTotalDays()
Dim startDate As Date
Dim endDate As Date
Dim totalDays As Integer

' Check if both start and end dates are valid


If IsDate(Me.LeaveStartDateTextBox.value) And IsDate(Me.LeaveEndDateTextBox.value) Then
startDate = DateValue(Me.LeaveStartDateTextBox.value)
endDate = DateValue(Me.LeaveEndDateTextBox.value)

' Calculate the total days, and add 1 to include the end date
totalDays = DateDiff("d", startDate, endDate) + 1

' Display the total days in the TotalDaysLabel


Me.TotalDaysLabel.Caption = "Total Days: " & totalDays
Else
' Clear the TotalDaysLabel if dates are invalid
Me.TotalDaysLabel.Caption = "Total Days: "
End If
End Sub

Sub InitializeListViewColumns()
' Initialize the ListView control with columns
With Me.DataShowListView
' Clear any existing columns
.ColumnHeaders.Clear

' Add columns to the ListView based on the new order of headings
.ColumnHeaders.Add , , "Serial Number", 50 ' Add a column for serial number
.ColumnHeaders.Add , , "Employee ID", 70
.ColumnHeaders.Add , , "Employee Name", 200
.ColumnHeaders.Add , , "Company Joining Date", 100
.ColumnHeaders.Add , , "Position", 100
.ColumnHeaders.Add , , "Location", 100
.ColumnHeaders.Add , , "Leave Type", 100
.ColumnHeaders.Add , , "Leave Start Date", 100
.ColumnHeaders.Add , , "Leave End Date", 100
.ColumnHeaders.Add , , "Departure Date", 100
.ColumnHeaders.Add , , "Request Date", 100
.ColumnHeaders.Add , , "Total Days", 100
.ColumnHeaders.Add , , "Leave Balance", 100
.ColumnHeaders.Add , , "Joining Date", 100
.ColumnHeaders.Add , , "Paid Leave Days", 100
.ColumnHeaders.Add , , "Unpaid Leave Days", 100
.ColumnHeaders.Add , , "Leaves Expiring Next Year", 150
.ColumnHeaders.Add , , "Tickets Remaining", 100
.ColumnHeaders.Add , , "Ticket Remaining Years", 150
.ColumnHeaders.Add , , "Ticket Utilization Year", 100
.ColumnHeaders.Add , , "Family Ticket", 100
.ColumnHeaders.Add , , "Family Ticket Utilization", 100
.ColumnHeaders.Add , , "Remarks", 150

' Set other ListView properties


.View = lvwReport
.FullRowSelect = True
.Gridlines = True
.HideColumnHeaders = False
End With
End Sub

Sub PopulateListViewWithData(ByVal empID As String)


' Populate the ListView with data from "Leave & Ticket Request Log" for the specified employee ID
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Leave & Ticket Request Log")

Dim lastRow As Long


lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).row

Dim lvItem As ListItem


Dim i As Long
Dim serialNumber As Long ' Variable to keep track of the serial number

' Initialize the serial number


serialNumber = 1

' Loop through the rows in the worksheet and add data to the ListView
For i = 2 To lastRow ' Assuming the first row contains headers
If ws.Cells(i, 2).value = empID Then
' Add an item to the ListView and increment the serial number
Set lvItem = Me.DataShowListView.ListItems.Add(, , serialNumber) ' Serial Number
serialNumber = serialNumber + 1 ' Increment the serial number
lvItem.ListSubItems.Add , , ws.Cells(i, 2).value ' Employee ID
lvItem.ListSubItems.Add , , ws.Cells(i, 3).value ' Employee Name
lvItem.ListSubItems.Add , , ws.Cells(i, 4).value ' Company Joining Date
lvItem.ListSubItems.Add , , ws.Cells(i, 5).value ' Position
lvItem.ListSubItems.Add , , ws.Cells(i, 6).value ' Location
lvItem.ListSubItems.Add , , ws.Cells(i, 7).value ' Leave Type
lvItem.ListSubItems.Add , , ws.Cells(i, 8).value ' Leave Start Date
lvItem.ListSubItems.Add , , ws.Cells(i, 9).value ' Leave End Date
lvItem.ListSubItems.Add , , ws.Cells(i, 10).value ' Departure Date
lvItem.ListSubItems.Add , , ws.Cells(i, 11).value ' Request Date
lvItem.ListSubItems.Add , , ws.Cells(i, 12).value ' Total Days
lvItem.ListSubItems.Add , , ws.Cells(i, 13).value ' Leave Balance
lvItem.ListSubItems.Add , , ws.Cells(i, 14).value ' Joining Date
lvItem.ListSubItems.Add , , ws.Cells(i, 15).value ' Paid Leave Days
lvItem.ListSubItems.Add , , ws.Cells(i, 16).value ' Unpaid Leave Days
lvItem.ListSubItems.Add , , ws.Cells(i, 17).value ' Leaves Expiring Next Year
lvItem.ListSubItems.Add , , ws.Cells(i, 18).value ' Total Tickets Remaining
lvItem.ListSubItems.Add , , ws.Cells(i, 19).value ' Tickets Remaining (Years)
lvItem.ListSubItems.Add , , ws.Cells(i, 20).value ' Ticket Utilization Year
lvItem.ListSubItems.Add , , ws.Cells(i, 21).value ' Family Ticket
lvItem.ListSubItems.Add , , ws.Cells(i, 22).value ' Family Ticket Utilization
lvItem.ListSubItems.Add , , ws.Cells(i, 23).value ' Remarks
End If
Next i
End Sub

Private Sub SubmitButton_Click()


Dim ws As Worksheet
Dim lastRow As Long

' Set the worksheet where you want to save the data
Set ws = ThisWorkbook.Worksheets("Leave & Ticket Request Log")

' Find the last used row in the worksheet


lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).row + 1 ' Find the next available row

' Increment the "Srl #" automatically


Dim srlNumber As Long
srlNumber = ws.Cells(lastRow - 1, 1).value + 1

' Update the "Srl #" column with the new value
ws.Cells(lastRow, 1).value = srlNumber

' Get the values from the form controls and save them in the respective columns
ws.Cells(lastRow, 2).value = Me.EmployeeIDTextBox.value
ws.Cells(lastRow, 3).value = Me.EmployeeNameLabel.Caption
ws.Cells(lastRow, 4).value = Me.DateOfJoiningLabel.Caption
ws.Cells(lastRow, 5).value = Me.PositionLabel.Caption
ws.Cells(lastRow, 6).value = Me.LocationLabel.Caption
ws.Cells(lastRow, 7).value = Me.LeaveTypeComboBox.value
ws.Cells(lastRow, 8).value = Me.LeaveStartDateTextBox.value
ws.Cells(lastRow, 9).value = Me.LeaveEndDateTextBox.value
ws.Cells(lastRow, 10).value = Me.DepartureDateTextBox.value
ws.Cells(lastRow, 11).value = Me.RequestDateTextBox.value
' Extract and save the numeric value for "Total Days"
Dim totalDaysValue As String
totalDaysValue = Me.TotalDaysLabel.Caption
totalDaysValue = Replace(totalDaysValue, "Total Days:", "") ' Remove the "Total Days:" text
ws.Cells(lastRow, 12).value = Val(totalDaysValue) ' Convert to numeric and save

' Extract and save the numeric value for "Leave Balance"
Dim leaveBalanceValue As String
leaveBalanceValue = Me.LeaveBalanceLabel.Caption
leaveBalanceValue = Replace(leaveBalanceValue, "Total Days:", "") ' Remove the "Total Days:" text
ws.Cells(lastRow, 13).value = Val(leaveBalanceValue) ' Convert to numeric and save

ws.Cells(lastRow, 14).value = Me.JoiningDateTextBox.value

' Extract and save the numeric value for "Paid Leave Days"
Dim paidLeavesValue As String
paidLeavesValue = Me.PaidLeavesLabel.Caption
paidLeavesValue = Replace(paidLeavesValue, "Total Days:", "") ' Remove the "Total Days:" text
ws.Cells(lastRow, 15).value = Val(paidLeavesValue) ' Convert to numeric and save

' Extract and save the numeric value for "Unpaid Leave Days"
Dim unpaidLeavesValue As String
unpaidLeavesValue = Me.UnpaidLeavesLabel.Caption
unpaidLeavesValue = Replace(unpaidLeavesValue, "Total Days:", "") ' Remove the "Total Days:" text
ws.Cells(lastRow, 16).value = Val(unpaidLeavesValue) ' Convert to numeric and save

' Extract and save the numeric value for "Leaves Expiring Next Year"
Dim leavesExpiringValue As String
leavesExpiringValue = Me.LeavesExpiringLabel.Caption
leavesExpiringValue = Replace(leavesExpiringValue, "Total Days:", "") ' Remove the "Total Days:" text
ws.Cells(lastRow, 17).value = Val(leavesExpiringValue) ' Convert to numeric and save

ws.Cells(lastRow, 18).value = Me.TicketRemainingLabel.Caption


ws.Cells(lastRow, 19).value = Me.TicketRemYrLabel.Caption
ws.Cells(lastRow, 20).value = Me.TicketUtilizationComboBox.value
ws.Cells(lastRow, 21).value = Me.FamilyTicketLabel.Caption
ws.Cells(lastRow, 22).value = Me.FamilyTicketUtlzComboBox.value
ws.Cells(lastRow, 23).value = Me.RemarksTextBox.value

' Optionally, clear the form controls after submission


Me.EmployeeIDTextBox.value = ""
Me.EmployeeNameLabel.Caption = ""
Me.DateOfJoiningLabel.Caption = ""
Me.PositionLabel.Caption = ""
Me.LocationLabel.Caption = ""
Me.LeaveStartDateTextBox.value = ""
Me.LeaveEndDateTextBox.value = ""
Me.DepartureDateTextBox.value = ""
Me.RequestDateTextBox.value = ""
Me.LeaveTypeComboBox.value = ""
Me.JoiningDateTextBox.value = ""
Me.StatusLabel.Caption = ""
Me.TotalDaysLabel.Caption = "Total Days: "
Me.LeaveBalanceLabel.Caption = "Total Days: "
Me.PaidLeavesLabel.Caption = "Paid Leave Days: "
Me.UnpaidLeavesLabel.Caption = "Unpaid Leave Days: "
Me.LeavesExpiringLabel.Caption = ""
Me.TicketRemainingLabel.Caption = ""
Me.TicketRemYrLabel.Caption = ""
Me.TicketUtilizationComboBox.value = ""
Me.FamilyTicketLabel.Caption = ""
Me.FamilyTicketUtlzComboBox.value = ""
Me.RemarksTextBox.value = ""
' Clear other form controls as needed

' Inform the user that the data has been saved
MsgBox "Data has been successfully saved.", vbInformation, "Data Saved"

End Sub

Private Sub ResetButton_Click()


' Clear all the fields on the UserForm
Me.EmployeeIDTextBox.value = ""
Me.EmployeeNameLabel.Caption = ""
Me.DateOfJoiningLabel.Caption = ""
Me.PositionLabel.Caption = ""
Me.LocationLabel.Caption = ""
Me.LeaveStartDateTextBox.value = ""
Me.LeaveEndDateTextBox.value = ""
Me.DepartureDateTextBox.value = ""
Me.RequestDateTextBox.value = ""
Me.LeaveTypeComboBox.value = ""
Me.JoiningDateTextBox.value = ""
Me.StatusLabel.Caption = ""
Me.TotalDaysLabel.Caption = "Total Days: "
Me.LeaveBalanceLabel.Caption = "Total Days: "
Me.PaidLeavesLabel.Caption = "Paid Leave Days: "
Me.UnpaidLeavesLabel.Caption = "Unpaid Leave Days: "
Me.LeavesExpiringLabel.Caption = ""
Me.TicketRemainingLabel.Caption = ""
Me.TicketRemYrLabel.Caption = ""
Me.TicketUtilizationComboBox.value = ""
Me.FamilyTicketLabel.Caption = ""
Me.FamilyTicketUtlzComboBox.value = ""
Me.RemarksTextBox.value = ""

' Clear the items in the DataShowListView


Dim i As Long
For i = Me.DataShowListView.ListItems.Count To 1 Step -1
Me.DataShowListView.ListItems.Remove i
Next i

End Sub

Private Sub DownloadButton_Click()


' Create a new instance of Excel
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")

' Create a new workbook in the new Excel instance


Dim xlWb As Object
Set xlWb = xlApp.Workbooks.Add

' Export data to the new workbook


Dim ws As Object
Set ws = xlWb.Sheets(1)

' Copy headers


For col = 1 To DataShowListView.ColumnHeaders.Count
ws.Cells(1, col).value = DataShowListView.ColumnHeaders(col).Text
Next col

' Copy data


Dim lvItem As Object
Dim lvSubItem As Object
Dim row As Long

For Each lvItem In DataShowListView.ListItems


row = row + 1
col = 0 ' Reset column index
' Copy serial number (assuming it's in the first column)
col = col + 1
ws.Cells(row + 1, col).value = row

' Copy data sub-items


For Each lvSubItem In lvItem.ListSubItems
col = col + 1
ws.Cells(row + 1, col).value = lvSubItem.Text
Next lvSubItem
Next lvItem

' Make the new Excel instance visible and interactive


xlApp.Visible = True
xlApp.UserControl = True

' Release objects


Set ws = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
End Sub

Private Sub EmployeeIDTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As


Integer)
If KeyCode = 13 Then ' Check if Enter key is pressed
UpdateStatusLabel
End If
End Sub

Private Sub UpdateStatusLabel()


Dim employeeID As String
Dim currentDataRange As Range
Dim oldDataRange As Range
Dim cell As Range
Dim found As Boolean

' Get the employee ID from the EmployeeIDTextBox


employeeID = EmployeeIDTextBox.Text

' Set the ranges for "Current Employee Data" and "Old Master Data" sheets
' Assuming "EmpID" is in column A of "Current Employee Data"
' and "EmpID (formula)" is in column B of "Old Master Data"
Set currentDataRange = ThisWorkbook.Sheets("Current Employee Data").Range("A:A")
Set oldDataRange = ThisWorkbook.Sheets("Old Master Data").Range("B:B")
' Check if the employee is found in the "Current Employee Data" range
For Each cell In currentDataRange
If cell.value = employeeID Then
StatusLabel.Caption = "ACTIVE"
StatusLabel.ForeColor = RGB(0, 128, 0) ' Green color
found = True
Exit For
End If
Next cell

' If not found in "Current Employee Data," check "Old Master Data" range
If Not found Then
For Each cell In oldDataRange
If cell.value = employeeID Then
StatusLabel.Caption = "RESIGNED"
StatusLabel.ForeColor = RGB(255, 0, 0) ' Red color
found = True
Exit For
End If
Next cell
End If

' If not found in either, keep the default "NO DATA AVAILABLE" status in black color
If Not found Then
StatusLabel.Caption = "NO DATA AVAILABLE"
StatusLabel.ForeColor = RGB(0, 0, 0) ' Black color
End If
End Sub

You might also like