VBA to Compare Two Worksheets Based on Multiple Columns and Output Results to User Built Template

goob90

New Member
Joined
Nov 12, 2021
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Hello everyone!



I am working on simplifying our employee benefit reconcile process at work. With over 500 employees and 16 different enrollment options, the current reconcile process is messy and prone to errors.



I've created a flow in Power Automate Desktop (PAD) that allows the user to upload a payroll report and carrier invoice. The flow cleans up the data from the two files and writes and saves it to an Excel Workbook. I plan to use PAD to manipulate Macro Enabled Worksheet and heavily reduce the amount of user input.



That being said, the workbook has two sheets, a payroll report and carrier invoice.



I am wanting to use VBA/Macro to compare both worksheets and export the variances to a template I built. I know there are a ton of posts on comparing two sheets, but I am needing the VBA to look at several columns to make comparisons.



For instance, an employee may be listed twice on the payroll report. Once for their health premium and once for their dental. The same can be said about the carrier invoice.



I need the VBA to look at the "Employee ID" column between both sheet and then look at the "Product Column" to see if it's for health or dental. Once it matches those two criteria, I need it to look at the "Cost" to see if there is a variance.



There should only be 3 things that would cause an employee to be on the variance sheet:

  • The employee is on the payroll report but not on the invoice.
  • The employee is on the invoice but not on the payroll report.
  • There is a difference in what payroll deduction amount and what was on the carrier invoice. (Ideally, I would have it only show differences that were greater than$1.00)

Sheet One- Payroll Report
Sample Reconcile (1).xlsx
ABCDEFG
1First NameLast NameEmployee IDProduct Employee Portion Employer Portion Total Premium
2LukeSkywalker1976Health$ 25.00$ 475.00$ 500.00
3LukeSkywalker1976Dental$ 4.50$ 25.50$ 30.00
4Egon Spengler1984Dental$ 4.50$ 25.50$ 30.00
5LloydChristmas1994Health$50$650$700
6LloydChristmas1994Dental$6$34$40
7Clark Griswald1983Health$80$ 820.00$900
Sample Payroll Report
Cell Formulas
RangeFormula
G2:G7G2=SUM(E2,F2)


Sheet Two- Carrier Invoice
Sample Reconcile (1).xlsx
ABCDE
1First NameLast NameEmployee IDProduct Total Premium
2LukeSkywalker1976Health$ 600.00
3LukeSkywalker1976Dental$ 30.00
4Egon Spengler1984Dental$ 30.00
5LloydChristmas1994Health$ 700.00
6LloydChristmas1994Dental$ 40.00
7Peter McCallister1990Health$ 800.00
8Peter McCallister1990Dental$ 100.00
Sample Carrier Invoice


New Workbook with Results
Sample Reconcile (1).xlsx
ABCDEFGH
1First NameLast NameSSNProduct Payroll Deduction Inoivce Amount DifferenceNotes
2LukeSkywalker1976Health$ 1,976.00$ 600.00$ (1,376.00)
3Clark Griswald1983Health$ 900.00$ (900.00)Missing from Invoice
4Peter McCallister1990Health$ 800.00$ 800.00Missing Payroll Deduction
5Peter McCallister1990Dental$ 100.00$ 100.00Missing Payroll Deduction
Reconcile
Cell Formulas
RangeFormula
E2E2=SUM(C2,D2)
G2:G5G2=F2-E2
 
You're welcome, I was happy to help. Thanks for the feedback!

Please mark the post that answered your question as the solution to help future readers. Little check mark icon on the right side of the post.
This code has been so helpful for me at work, thank you again!

I have several values where the final difference is -0.01. Is there a way to exclude those from being flagged as "Invoice/Deduction Discrepancy"?

Basically, our deduction is a penny off from our invoice. It's not a huge deal, but it would be nice to leave those employees off.

I looked at the code and tried to figure it out myself with no luck. I'm guessing it would be somewhere in this area? If arrA(i, 7) <> 0 Then arrA(1, 8) = "Invoice/Deduction Discrepancy"

Test Reconcile VBA 2.xlsm
ABCDEFGH
2LukeSkywalker1976Health599.99600-0.01Invoice/Deduction Discrepancy
Reconcile
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Without going through the code again, if you replace the line you quoted
I'm guessing it would be somewhere in this area? If arrA(i, 7) <> 0 Then arrA(1, 8) = "Invoice/Deduction Discrepancy"
with this line, does it solve your issue
VBA Code:
If arrA(i, 7) > 0.01 Or arrA(i, 7) < -0.01 Then arrA(1, 8) = "Invoice/Deduction Discrepancy"
 
Upvote 0
Without going through the code again, if you replace the line you quoted

with this line, does it solve your issue
VBA Code:
If arrA(i, 7) > 0.01 Or arrA(i, 7) < -0.01 Then arrA(1, 8) = "Invoice/Deduction Discrepancy"
I want to say thank you again, your code has been a huge help to me! I am having a slight issue and I have tried to figure it out on my own, but I can't seem to fix it.

When the code is run with "Sample Payroll Report" and "Sample Carrier Invoice" worksheets having over 1000 rows, the resulting "Reconcile" sheet shows that some individuals are marked as "Missing From Invoice" and "Missing Payroll Deduction", even though they appear on both sheets. The problem seems to affect anyone on a row after 999. Any idea how I could fix it?
 
Upvote 0
I want to say thank you again, your code has been a huge help to me! I am having a slight issue and I have tried to figure it out on my own, but I can't seem to fix it.

When the code is run with "Sample Payroll Report" and "Sample Carrier Invoice" worksheets having over 1000 rows, the resulting "Reconcile" sheet shows that some individuals are marked as "Missing From Invoice" and "Missing Payroll Deduction", even though they appear on both sheets. The problem seems to affect anyone on a row after 999. Any idea how I could fix it?
Scratch that. The issue isn't with being over 1000 rows. I ran tests using dummy data with over 1000 rows with no issues. It just so happened that people showing Missing From Invoice" and "Missing Payroll Deduction" were listed after row 1000. I am not sure what the issue is, though I suspect that it would have to be in the formatting somewhere...
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top