VBA code to pick data based on multiple conditions

tirumal

Board Regular
Joined
Feb 16, 2020
Messages
50
Office Version
  1. 2016
Platform
  1. Windows
Hello guys,



I've been struggling to write this for days macro and I have finally decided to ask for your help.


Here's what I have:



Sheet1 - Job IDs, Assignees and Tasks as headers



A1 - IDs

B1 - Assignees

C1 - Tasks

*Rest of the columns are something i've tried which did not work out.



There are different no. of Jobs in the data and each job has 2 tasks only. The tasks (task1 and task2) of each job are done by either 2 assignees separately or done by the same assignee (for some jobs).



What I am trying to achieve:



- I want to filter data in such a way that the macro should pick only five task1's and task2's of each assignee.

- But ideally, when a task2 of a job is picked, task 1 of the same job should also be picked keeping in mind that the assignee that did that task did not have more than 5 task1;s (or) task2's picked.

- My idea is, the macro should be able to ideally keep a log or delete data no. of tasks of a particular assignee once they reach the threshold of 5.

- To put it in simple terms, the no. of jobs picked do not matter, but both task 1 and 2 of any Job should be picked and each assignee should equally have 5 task1 and task2's.



I had multiple attempts but I have failed miserably. I would be really grateful if someone helps me out.

*Please find the file link here

Thank you,
Tirumal
 
Debug.Print writes to the Immediate Window
It is a useful tool when trying to see what is going on with variables and values etc, especially when looping through many cells
The Immediate Window is displayed (when in VBA editor) using shortcut {CTRL} g or by clicking on menu tab View and then Immediate Window

To return items being rejected due to either count of items exceeding 5
... remove the apostrophe before these 2 lines in the latest version of the code
VBA Code:
                        'Debug.Print "failed CountIf",r, ID, Mgr1, Opt1
                        'Debug.Print "failed CountIf",r, ID2, Mgr2, Opt2
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi, Thanks for getting back :)
Your code is doing wonders as per allocation.
I've changed the number from maximum number 5 to 49 and ran the code on whole data.
The result is coming out as desired for most of the cases. But i'm not sure why this is happening:

Please check the yellow and red marks (E and F columns contain entire original True and false data as per manager):

Allocation test.xlsm
ABCDEF
30mtshah492675512440
31nikumark49499878512
32panchada494998122400
33pnrhul494998175170
34rdnsh1523381523
35rogelios61218612
36saithom494796368261
37sanikh494998454389
38sattili494998184233
39shprakha494998110324
40siaditi49499887413
41skaveesh493988430445
42smantral494998136147
43snghoj494998986372
44sshaka494998395278
45tulap494998175217
46tvinaya11112111
47vnchan494998228157
48wajakhan494998256355
49Grand Total1928193438621190511905
Sheet4


- The true and false are not equally distributed in the end and people marked in yellow have a huge variance b/w true and false where false is much lower. False data cannot be lower than true data when there's enough samples in the original data right?
- Is this because of any data limitation ?
or the value 49 i've put ?
 
Upvote 0
The code is working correctly - but is working through JOB by JOB
By cancelling it after 50 you get the first found 50 jobs
In the meantime run the sub without your 50 restriction
I have an idea - will update thread shortly
 
Upvote 0
Sorry - I just realised I misunderstood what you said last time - ignore previous reply
 
Upvote 0
Reason for putting 50 is because i need the data to be allocated for same (50-60) or more sometimes.
The code is running perfectly but
The true and false allocation itself is not equal. Also False data (Column C) is lower for some people.

Allocation test.xlsm
ABCD
39saithom504898
40ayumishr504191
41skaveesh504090
42biswadm503585
43elisaty 323466
44mtshah502777
45rdnsh152338
46rogelios61218
47tvinaya11112
48minhaza11
49Grand Total196619723938
Sheet6


I'm guessing if it's data limitation ?
Will putting extra data help?
 
Upvote 0
I am currently testing something that may help you
 
Upvote 0
I have found an approach that may work and will update the thread within the next 2 hours after I have fully tested revised code
 
Upvote 0
The code ran much too slowly when tested
I will revert when I have resolved that issue
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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