Here's my first VBA project. I have some very limited Python experience, and this is my first VBA project. I'm sure I could have done it a lot more simply, but I just stuck with what I knew, and googled what I didn't, so feedback is very welcome.
The basic purpose is to compare a table from one sheet (OP) with a table from another (Payroll) and print the discrepancies in a third sheet (Results).
One last thing I want to do, but can't figure out (without using GoTo, which I read is bad) is to protect the worksheets after I Exit Sub on lines 22, 31, etc. (code for protecting sheets is already written in line 130 - 138.
Sub Macro1()
Dim counter As Integer
Set OPdata = New Dictionary
Set Payrolldata = New Dictionary
Set HRIDnames = New Dictionary
'Unprotects sheets
Worksheets("Results").Unprotect Password:=""
Worksheets("OP").Unprotect Password:=""
Worksheets("Payroll").Unprotect Password:=""
' Clears Results worksheet
Worksheets("Results").Activate
Range("A3:L10000").ClearContents
' Looks for missing data in the OP worksheet
Worksheets("OP").Activate
Range("A2").Select
If IsEmpty(ActiveCell) Then
MsgBox "There is data missing"
Exit Sub
End If
Worksheets("OP").Activate
Range("A1").CurrentRegion.Select
For Each C In Selection
If C = "" Then
C.Select
MsgBox "There is data missing"
Exit Sub
End If
Next
' Looks for missing data in the Payroll worksheet
Worksheets("Payroll").Activate
Range("A2").Select
If IsEmpty(ActiveCell) Then
MsgBox "There is data missing"
Exit Sub
End If
Worksheets("Payroll").Activate
Range("A1").CurrentRegion.Select
For Each C In Selection
If C = "" Then
C.Select
MsgBox "There is data missing"
Exit Sub
End If
Next
' Populate OPdata dictionary
Worksheets("OP").Activate
Range("A2", ActiveCell.End(xlDown)).Select
For Each cl In Selection
OPdata.Add cl.Value, cl.Offset(0, 2).Value & " " & cl.Offset(0, 3).Value
HRIDnames.Add cl.Value, cl.Offset(0, 1).Value
Next
' Populate Payrolldata dictionary
Worksheets("Payroll").Activate
Range("A2", ActiveCell.End(xlDown)).Select
For Each cl In Selection
Payrolldata.Add cl.Value, cl.Offset(0, 2).Value & " " & cl.Offset(0, 3).Value
If HRIDnames.Exists(cl.Value) = False Then
HRIDnames.Add cl.Value, cl.Offset(0, 1).Value
End If
Next
' finds unique values in OPdata and prints them to columns A:C
Worksheets("Results").Activate
Range("A3").Select
For Each i In OPdata
If Payrolldata.Exists(i) = False Then
ActiveCell = i
ActiveCell.Offset(0, 2).Select
ActiveCell = OPdata.Item(i)
ActiveCell.Offset(1, -2).Select
End If
Next
' finds unique values in Payrolldata and prints them to columns E:G
Worksheets("Results").Activate
Range("E3").Select
For Each i In Payrolldata
If OPdata.Exists(i) = False Then
ActiveCell = i
ActiveCell.Offset(0, 2).Select
ActiveCell = Payrolldata.Item(i)
ActiveCell.Offset(1, -2).Select
End If
Next
' Finds salary discrepencies and prints them to columns I:L
Worksheets("Results").Activate
Range("I3").Select
For Each i In OPdata
If Payrolldata.Exists(i) Then
If Not OPdata.Item(i) = Payrolldata.Item(i) Then
ActiveCell = i
ActiveCell.Offset(0, 2).Select
ActiveCell = OPdata.Item(i)
ActiveCell.Offset(0, 1).Select
ActiveCell = Payrolldata.Item(i)
ActiveCell.Offset(1, -3).Select
End If
End If
Next
' Prints names
Sheets("Results").Range("A3").Select
While Not IsEmpty(ActiveCell)
ActiveCell.Offset(0, 1) = HRIDnames.Item(ActiveCell.Value)
ActiveCell.Offset(1).Select
Wend
Sheets("Results").Range("E3").Select
While Not IsEmpty(ActiveCell)
ActiveCell.Offset(0, 1) = HRIDnames.Item(ActiveCell.Value)
ActiveCell.Offset(1).Select
Wend
Sheets("Results").Range("I3").Select
While Not IsEmpty(ActiveCell)
ActiveCell.Offset(0, 1) = HRIDnames.Item(ActiveCell.Value)
ActiveCell.Offset(1).Select
Wend
' Reprotect Worksheets
Worksheets("Results").Protect Password:=""
Worksheets("OP").Protect Password:=""
Worksheets("Payroll").Protect Password:=""
' Success message
If WorksheetFunction.CountA(Range("A3:L3")) = 0 Then
MsgBox ("Congratulations! OurPeople and Payroll reconcile exactly!")
End If
End Sub