0% found this document useful (0 votes)
11 views3 pages

Air Ticket Payroll Provision Report

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)
11 views3 pages

Air Ticket Payroll Provision Report

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/ 3

Sub GenerateAirTicketReport()

Dim folderPath As String


Dim fileName As String
Dim filePath As String
Dim empNo As String
Dim empNationality As String
Dim memberCount As String
Dim provisionAmount As String
Dim reportRow As Long
Dim pos1 As Long, pos2 As Long, pos3 As Long, pos4 As Long
Dim folderPicker As FileDialog
Dim reportWorkbook As Workbook
Dim reportSheet As Worksheet
Dim sourceWorkbook As Workbook
Dim sourceSheet As Worksheet
Dim sourceFilePath As String
Dim timeStamp As String
Dim empRow As Range

' Prompt the user to select the folder


Set folderPicker = Application.FileDialog(msoFileDialogFolderPicker)
With folderPicker
.Title = "Select Folder Containing PDF Files"
.AllowMultiSelect = False
If .Show = -1 Then
folderPath = .SelectedItems(1) & "\"
Else
MsgBox "No folder selected. Exiting..."
Exit Sub
End If
End With

' Fixed path to the source file


sourceFilePath = "\\172.16.12.15\groups\HR & Admin\EVO-HR & Admin\
Administration\T&A\EVO Master.xlsm"

' Open the source file without updating links, in read-only mode if necessary
On Error Resume Next
Set sourceWorkbook = Workbooks.Open(sourceFilePath, UpdateLinks:=0,
ReadOnly:=True)
If Err.Number <> 0 Then
MsgBox "Error opening source file. Exiting..."
Exit Sub
End If
On Error GoTo 0
Set sourceSheet = sourceWorkbook.Sheets(1)

' Create a new workbook for the report


Set reportWorkbook = Workbooks.Add
Set reportSheet = reportWorkbook.Sheets(1)

' Initialize the report row


reportRow = 2

' Set up the report headers


With reportSheet
.Cells(1, 1).Value = "Emp. No"
.Cells(1, 2).Value = "Name"
.Cells(1, 3).Value = "Designation"
.Cells(1, 4).Value = "Department"
.Cells(1, 5).Value = "Nationality"
.Cells(1, 6).Value = "Eligible Count"
.Cells(1, 7).Value = "Provision Amount"
End With

' Loop through each file in the folder


fileName = Dir(folderPath & "*.pdf")
Do While fileName <> ""
' Get the full file path
filePath = folderPath & fileName

' Parse the file name


pos1 = InStr(1, fileName, "-")
pos2 = InStr(pos1 + 1, fileName, "-")
pos3 = InStr(pos2 + 1, fileName, "-")
pos4 = InStr(pos3 + 1, fileName, "-")

If pos1 > 0 And pos2 > 0 And pos3 > 0 And pos4 > 0 Then
empNo = Mid(fileName, 1, pos1 - 1)
empNationality = Mid(fileName, pos1 + 1, pos2 - pos1 - 1)
memberCount = Mid(fileName, pos2 + 1, pos3 - pos2 - 1)
provisionAmount = Mid(fileName, pos3 + 1, pos4 - pos3 - 1)

' Fetch employee details from the source file


Set empRow = sourceSheet.Columns(2).Find(empNo, LookIn:=xlValues,
LookAt:=xlWhole)
If Not empRow Is Nothing Then
' Write the data to the report
With reportSheet
.Cells(reportRow, 1).Value = empNo
.Cells(reportRow, 2).Value = empRow.Offset(0, 4).Value ' Name
.Cells(reportRow, 3).Value = empRow.Offset(0, 6).Value '
Designation
.Cells(reportRow, 4).Value = empRow.Offset(0, 7).Value '
Department
.Cells(reportRow, 5).Value = empRow.Offset(0, 10).Value '
Nationality
.Cells(reportRow, 6).Value = memberCount
.Cells(reportRow, 7).Value = Round(CDbl(provisionAmount), 0)
End With
Else
MsgBox "Employee number not found in source file: " & empNo
End If

' Move to the next row in the report


reportRow = reportRow + 1
Else
MsgBox "File name format is incorrect: " & fileName
End If

' Get the next file


fileName = Dir
Loop

' Apply cell borders and auto-fit columns


With reportSheet
.Columns("G").NumberFormat = "_(* #,##0_);_(* (#,##0);_(* -_);_(@_)"
.Range("A1:G" & reportRow - 1).Borders.LineStyle = xlContinuous
.Columns("A:G").AutoFit
End With

' Generate the timestamp for the file name


timeStamp = Format(Now, "yyyymmdd_hhmmss")

' Save and close the report


reportWorkbook.SaveAs folderPath & "AirTicketReport_" & timeStamp & ".xlsx"
reportWorkbook.Close
sourceWorkbook.Close False

MsgBox "Report generated and saved successfully in the same folder where you
selected the month!"
End Sub

You might also like