VBA: Analyze/Compare data from two worksheets to programatically insert data from one into another.

Engineer Joe

Well-known Member
Joined
Jun 10, 2005
Messages
549
Ok, here's the setup: Two worksheets. Worksheet A (Which is a collection data representing monthly labor projections) and Worksheet B (Which is a list of actual labor expended). The goal is to take all the actual labor from Worksheet B and place it into it's own column (maybe row too) in Worksheet A. There's a reason this needs to be done by code, and that is because of 4 parameters. These four parameters (variables really) exist in every row of each sheet. The are Division, Project, Labor Type, and Employee. So, here's the deal. Some of the data in Worksheet B was previously hand-entered into a program and then extracted as an excel worksheet, which means there could be slight manual labors - which I will attempt to correct. I'll take care of that part later, but needs to be acknowledged from the beginning.

Essentially, I need to match the 4 parameters of each row in Worksheet B to those of a row in Worksheet A. When all four are a match, I simply plop the "actual" labor efforts into the matching row in Worksheet A. Sounds pretty simple right? But wait, there's more. Here's where it gets fuzzy. For any row in worksheet A compared to any row in worksheet B, I actually need to know which of those 4 parameters are a match. This, when you consider it, can be represented by 16 different scenarios (or 4 bits) - Anywhere from none of the parameters match (0000) to all of the parameters match (1111). The reason I need to know this is that there's actually a hierarchy or prioritization of matches. I need for the parameters each given row in Worksheet B to search each row in Worksheet A and find the best match (it won't always yield a 1111). There could be several matches, but no complete matches. Based on the best match (which might be 1101 or 0101 or 1110 or whatever), I already know how I would like to prioritize and react to the matches. ALSO, no two rows in Worksheet A will ever return the same results to a search from any given row in Worksheet B.

Anyway, I started out as basically saying something like this (and this is not the actual code at all...this is a transcription of structure. I'm not asking about syntax, but about how best to structure it):
Code:
for each rw in Worksheet B
     
     for each rw in Worksheet A
          if Division parameter matches Worksheet B row division parameter, then add that range to a range array representing a collection of ranges with matching Division parameters     
          if Labor Type parameter matches Worksheet B row labor type parameter, then add that range to a range array representing a collection of ranges with matching Labor type parameters
          if Project parameter matches Worksheet B row Project parameter, then add that range to a range array representing a collection of ranges with matching Project parameters          
          if Employee parameter matches Worksheet B row Employee parameter, then add that range to a range array representing a collection of ranges with matching Employee parameters.
     next rw
     'So now I have 4 range arrays, each representing for the given parameter a collection of ranges in wb A that match it.
     'But now I don't know what to do.   How do I compare between the 4 arrays to find the best match?  I already know my preferred heirarchy of partial matches, identifying what the highest order 
      'match is.  Like I said before, there are sixteen possible scenarios after going through each row in Worksheet A - anything between no parameter matches in any rows to all four parameters 
      'perfectly matching a row in worksheet A.  So obviously, every row in A will return any one of those 16 scenarios.  This is where I'm stuck.  I've already written the select case statement for when
      'the best match is made....I just don't know how to identify it. :(

next rw
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,215,022
Messages
6,122,721
Members
449,093
Latest member
Mnur

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