Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Macros for Checklist

1. For Action Performed – Done Clicker:

Sub FillOneCellAtATimeCol3()
ActiveSheet.Unprotect Password:=""
Dim username As String
username = Environ("Username")

Dim currentCell As Range


Set currentCell = Range("E3")

' Check if the username already exists in the row


If Not IsUsernameInRow(currentCell, username) Then
If IsEmpty(currentCell.Value) Then
currentCell.Value = username
Else
Do While Not IsEmpty(currentCell.Offset(0, 1).Value)
Set currentCell = currentCell.Offset(0, 1)
Loop
currentCell.Offset(0, 1).Value = username
End If
End If
ActiveSheet.Protect Password:=""
End Sub

Function IsUsernameInRow(startCell As Range, username As String) As Boolean


Dim currentCell As Range
Set currentCell = startCell

' Loop through the row to check if the username already exists
Do While Not IsEmpty(currentCell.Value)
If currentCell.Value = username Then
IsUsernameInRow = True
Exit Function
End If
Set currentCell = currentCell.Offset(0, 1)
Loop

IsUsernameInRow = False
End Function

2. UID – Name Macro:


Sub PopulateNamesWithHyphen()
ActiveSheet.Unprotect Password:=""
Dim ws1 As Worksheet, ws2 As Worksheet
Dim employeeIDCell As Range

' Set the worksheets


Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")

' Check if there is data in cells E3 to L29 in Sheet1


If WorksheetFunction.CountA(ws1.Range("E3:L29")) > 0 Then
' Loop through selected cells in column A of Sheet1
For Each employeeIDCell In ws1.Range("E3:L29").SpecialCells(xlCellTypeConstants)
' Find the corresponding name in Sheet2
On Error Resume Next
Dim name As Variant
name = Application.WorksheetFunction.VLookup(employeeIDCell.Value, ws2.Range("A:B"),
2, False)
On Error GoTo 0

' Check if a name is found before populating


If Not IsError(name) Then
' Populate the cell in Sheet1 with "EmployeeID - Name"
employeeIDCell.Value = employeeIDCell.Value & " - " & name
Else
MsgBox "Name not found for Employee ID: " & employeeIDCell.Value, vbExclamation
End If
Next employeeIDCell

Else
' No data in cells E3 to L29, provide a message or take appropriate action
MsgBox "UID-NAME POPULATION REJECTED. Error: Incomplete Checklist.",
vbExclamation
End If
ActiveSheet.Protect Password:=""
End Sub

3. Manager Sign-off Macro:

Sub PopulateManagerInfo()
ActiveSheet.Unprotect Password:=""
Dim ws1 As Worksheet, ws2 As Worksheet
Dim employeeID As String, managerID As String, managerName As String
Dim teamRange As Range, teamMember As Range
Dim currentUser As String
' Set references to the worksheets
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")

' Hide column M


ws1.Columns("N").Hidden = True

' Get the current system username


currentUser = Environ("Username")

' Check if the current user is designated as a manager in Sheet2


If IsManager(currentUser, ws2) Then
' Check if there is data in cells E3 to L29 in Sheet1
If WorksheetFunction.CountA(ws1.Range("E3:L29")) > 0 Then
' Get the employee ID from Sheet1 Q5
employeeID = ws2.Cells(2, 1).Value

' Find the employee in Sheet2


Set teamRange = ws2.Range("A:C")
Set teamMember = teamRange.Find(employeeID, LookIn:=xlValues)

If Not teamMember Is Nothing Then


' Employee found, get manager details
managerID = teamMember.Offset(0, 0).Value ' Manager's ID in column 2
managerName = teamMember.Offset(0, 1).Value ' Manager's Name in column 3

' Populate Q5 in Sheet1 with the manager's ID and name


ws1.Cells(5, 18).Value = managerID & " - " & managerName

' Populate Q6 in Sheet1 with today's date


ws1.Cells(6, 18).Value = Date

Call PopulateNamesWithHyphen

' Display message box


MsgBox "CMWT Checklist officially Signed off!"
Else
' Employee not found, provide a message or take appropriate action
MsgBox "Employee ID not found in the team list."
End If
Else
' No data in cells E3 to L29, provide a message or take appropriate action
MsgBox "No data in cells E3 to L29. Please fill in the required information."
End If
Else
MsgBox "You are not designated as the manager. Access denied.", vbExclamation
End If
ActiveSheet.Protect Password:=""
End Sub
Function IsManager(username As String, ws As Worksheet) As Boolean
' Check if the given username is designated as a manager in Sheet2
Dim managerRange As Range
Set managerRange = ws.Range("A:C").Find(username, LookIn:=xlValues)
IsManager = Not managerRange Is Nothing And managerRange.Offset(0, 2).Value = "Manager" '
Assumes "Manager" is the designation in column 3
End Function
4. Clear Username in Row Macro:

Sub ClearContentBySystemUser()
Dim ws As Worksheet
Dim targetRow As Range
Dim currentUser As String
Dim cell As Range

' Set the worksheet


Set ws = ThisWorkbook.Sheets("Sheet1")

' Get the current system username


currentUser = Environ("Username")

' Set the target row range (E3 to L3)


Set targetRow = ws.Range("E3:L3")

' Loop through each cell in the target row


For Each cell In targetRow
' Check if the content matches the system username
If cell.Value = currentUser Then
' Clear the content if it matches
cell.ClearContents

' Shift content to the left in the same row


Dim i As Integer
For i = cell.Column - targetRow.Column + 1 To targetRow.Columns.Count - 1
targetRow.Cells(1, i).Value = targetRow.Cells(1, i + 1).Value
Next i
End If
Next cell
End Sub

You might also like