Practice Question on VBA
Practice Question on VBA
Questions
1. Write a VBA code to check if the salary of each employee is greater than $50,000 and if
their performance rating is 4 or higher.
2. Write a VBA code to flag employees whose age is either above 40 or who have more
than 12 years of experience.
3. Write a VBA code to find employees eligible for a bonus if their department is not IT and
they have a performance rating of 5.
4. Write a VBA code to highlight employees who are in the Finance department and have
either a performance rating of 3 or less or a salary below $60,000.
5. Write a VBA code using the XOR function to determine if an employee is either above
30 years of age or has a performance rating of 5, but not both.
Nested IF Statements
6. Write a VBA code to assign a bonus percentage: 10% if the salary is below $50,000 and
performance is 5, 7% if salary is $50,000-$70,000 and performance is 4 or above, and 5%
otherwise.
7. Write a VBA code to categorize employees into "Junior," "Mid-level," and "Senior"
based on their experience: less than 5 years as "Junior," 5-10 years as "Mid-level," and
more than 10 years as "Senior."
8. Write a VBA code to determine if an employee is eligible for a special project if they are
in the IT department and have a performance rating of 5 or if they are from the HR
department with experience greater than 7 years.
Custom Tasks
11. Write a VBA code to find employees with a specific name (e.g., "Alice") and determine
if their salary is above the department average.
12. Write a VBA code to calculate whether employees from the Sales department qualify for
a travel allowance if they have a performance rating of 4 or above and are below 35 years
of age.
13. Write a VBA code to mark employees for training if their performance rating is below 3,
they have less than 5 years of experience, or their salary is below $45,000.
14. Write a VBA code to calculate the department-wise average salary and find employees
whose salary is below the average for their department.
Advanced Analysis
15. Write a VBA code to flag employees who have the highest salary in their department.
16. Write a VBA code to determine whether an employee qualifies for a leadership role if
they have at least 15 years of experience or a performance rating of 5 and are above 35
years of age.
17. Write a VBA code to find employees who are not eligible for promotion but have a
performance rating above 3 and experience above 10 years.
Logical Comparisons
18. Write a VBA code to compare employees' salaries to the company-wide average and
categorize them as "Below Average," "Average," or "Above Average."
19. Write a VBA code to calculate the bonus as $1000 if the employee’s performance is 5,
$500 if the performance is 4, and $200 otherwise.
20. Write a VBA code to check if employees belong to either HR or IT departments and have
a salary greater than $60,000, and flag them for department-level rewards.
Here’s a detailed solution to each question with VBA code. I'll explain the approach and provide
the relevant VBA code for all 20 questions. To implement these, ensure your dataset is in a
worksheet named "Data" starting from cell A1.
Question 1
Check if the salary is greater than $50,000 and performance rating is 4 or higher.
Sub CheckSalaryPerformance()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 6).Value > 50000 And ws.Cells(i, 7).Value >= 4 Then
ws.Cells(i, 9).Value = "Eligible"
Else
ws.Cells(i, 9).Value = "Not Eligible"
End If
Next i
End Sub
Question 2
Flag employees whose age is either above 40 or who have more than 12 years of experience.
Sub FlagAgeOrExperience()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 4).Value > 40 Or ws.Cells(i, 5).Value > 12 Then
ws.Cells(i, 9).Value = "Flagged"
Else
ws.Cells(i, 9).Value = "OK"
End If
Next i
End Sub
Question 3
Find employees eligible for a bonus if their department is not IT and performance rating is
5.
Sub BonusEligibility()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 3).Value <> "IT" And ws.Cells(i, 7).Value = 5 Then
ws.Cells(i, 9).Value = "Bonus Eligible"
Else
ws.Cells(i, 9).Value = "Not Eligible"
End If
Next i
End Sub
Question 4
Sub HighlightFinance()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 3).Value = "Finance" And (ws.Cells(i, 7).Value <= 3 Or
ws.Cells(i, 6).Value < 60000) Then
ws.Cells(i, 9).Value = "Highlight"
Else
ws.Cells(i, 9).Value = "OK"
End If
Next i
End Sub
Question 5
Sub XORCondition()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If (ws.Cells(i, 4).Value > 30 Xor ws.Cells(i, 7).Value = 5) Then
ws.Cells(i, 9).Value = "Condition Met"
Else
ws.Cells(i, 9).Value = "Condition Not Met"
End If
Next i
End Sub
Question 6
Assign bonus percentages based on salary and performance.
Sub BonusPercentage()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 6).Value < 50000 And ws.Cells(i, 7).Value = 5 Then
ws.Cells(i, 10).Value = "10%"
ElseIf ws.Cells(i, 6).Value >= 50000 And ws.Cells(i, 6).Value <=
70000 And ws.Cells(i, 7).Value >= 4 Then
ws.Cells(i, 10).Value = "7%"
Else
ws.Cells(i, 10).Value = "5%"
End If
Next i
End Sub
Question 7
Sub CategorizeExperience()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 5).Value < 5 Then
ws.Cells(i, 10).Value = "Junior"
ElseIf ws.Cells(i, 5).Value >= 5 And ws.Cells(i, 5).Value <= 10 Then
ws.Cells(i, 10).Value = "Mid-level"
Else
ws.Cells(i, 10).Value = "Senior"
End If
Next i
End Sub
Question 8
Sub SpecialProjectEligibility()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If (ws.Cells(i, 3).Value = "IT" And ws.Cells(i, 7).Value = 5) Or
(ws.Cells(i, 3).Value = "HR" And ws.Cells(i, 5).Value > 7) Then
ws.Cells(i, 10).Value = "Eligible"
Else
ws.Cells(i, 10).Value = "Not Eligible"
End If
Next i
End Sub
Question 9
Sub PromotionAndIncrement()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 7).Value >= 4 And ws.Cells(i, 5).Value > 5 And
ws.Cells(i, 3).Value <> "Sales" Then
ws.Cells(i, 10).Value = "Eligible"
Else
ws.Cells(i, 10).Value = "Not Eligible"
End If
Next i
End Sub
Question 10
Sub CheckAnyCondition()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 4).Value >= 35 Or ws.Cells(i, 6).Value > 60000 Or
ws.Cells(i, 3).Value = "HR" Then
ws.Cells(i, 10).Value = "Condition Met"
Else
ws.Cells(i, 10).Value = "Condition Not Met"
End If
Next i
End Sub
Question 11
Find employees with a specific name and check salary against the department average.
Sub CheckSalaryAgainstDeptAverage()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim deptAverage As Double
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 2).Value = targetName Then
' Assuming a pre-calculated department average exists in column K
deptAverage = ws.Cells(i, 11).Value
If ws.Cells(i, 6).Value > deptAverage Then
ws.Cells(i, 10).Value = "Above Avg"
Else
ws.Cells(i, 10).Value = "Below Avg"
End If
End If
Next i
End Sub
Question 12
Sub TravelAllowanceEligibility()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 3).Value = "Sales" And ws.Cells(i, 7).Value >= 4 And
ws.Cells(i, 4).Value < 35 Then
ws.Cells(i, 10).Value = "Eligible"
Else
ws.Cells(i, 10).Value = "Not Eligible"
End If
Next i
End Sub
Question 13
Sub TrainingEligibility()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 7).Value < 3 Or ws.Cells(i, 5).Value < 5 Or
ws.Cells(i, 6).Value < 45000 Then
ws.Cells(i, 10).Value = "Training Needed"
Else
ws.Cells(i, 10).Value = "No Training"
End If
Next i
End Sub
Question 14
Sub BelowDeptAverage()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Question 15
Sub FlagHighestSalary()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim maxSalary As Double
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Question 16
Sub LeadershipEligibility()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 5).Value >= 15 Or (ws.Cells(i, 7).Value = 5 And
ws.Cells(i, 4).Value > 35) Then
ws.Cells(i, 10).Value = "Eligible"
Else
ws.Cells(i, 10).Value = "Not Eligible"
End If
Next i
End Sub
Question 17
Find employees not eligible for promotion but meeting other criteria.
Sub FindOtherCriteria()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 8).Value = "No" And ws.Cells(i, 7).Value > 3 And
ws.Cells(i, 5).Value > 10 Then
ws.Cells(i, 10).Value = "Meets Other Criteria"
Else
ws.Cells(i, 10).Value = "Does Not Meet"
End If
Next i
End Sub
Question 18
Check if an employee meets all three conditions: (1) Age > 30, (2) Experience ≥ 10 years,
and (3) Performance rating = 5.
Sub AllConditionsMet()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 4).Value > 30 And ws.Cells(i, 5).Value >= 10 And
ws.Cells(i, 7).Value = 5 Then
ws.Cells(i, 10).Value = "All Conditions Met"
Else
ws.Cells(i, 10).Value = "Conditions Not Met"
End If
Next i
End Sub
Question 19
Flag employees in the Sales department who have worked less than 3 years and have a
salary greater than $70,000.
Sub FlagSalesEmployees()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 3).Value = "Sales" And ws.Cells(i, 5).Value < 3 And
ws.Cells(i, 6).Value > 70000 Then
ws.Cells(i, 10).Value = "Flagged"
Else
ws.Cells(i, 10).Value = "Not Flagged"
End If
Next i
End Sub
Question 20
Identify employees who should be considered for a mentorship role based on experience
and performance.
Criteria:
Experience ≥ 10 years
Performance rating ≥ 4
Sub MentorshipEligibility()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 5).Value >= 10 And ws.Cells(i, 7).Value >= 4 Then
ws.Cells(i, 10).Value = "Mentorship Eligible"
Else
ws.Cells(i, 10).Value = "Not Eligible"
End If
Next i
End Sub