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
 
The code is failing on the first row
Is 9903 the correct value for the last row of data in new sheet?

If it is ....
Add a further 4 message boxes in the same place and let me know what all 6 message boxes say
MsgBox mT2 ,,"mT2"
MsgBox mF2 ,,"mF2"
MsgBox D2.Parent.Name & vbcr & D2.Address(0,0) ,,"D2"
MsgBox E2.Parent.Name & vbcr & E2.Address(0,0) ,,"E2"

I would expect the following values
2
9903
anukriti
abhyud
any sheet name and D2:D9904
any sheet name and E2:E9904
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Yes, the values are exactly the same!

2
9903
anukriti
abhyud
any sheet name and D2:D9904
any sheet name and E2:E9904
 
Upvote 0
Ah my apologies - copy\paste problem
The top of the code is missing 2 lines

Insert these 2 lines at the top of the module and replace the amended code with the original code
VBA Code:
Option Explicit
Dim wf As WorksheetFunction
 
Upvote 0
It has created an output like this:

Allocation test.xlsm
ABCDEFG
1Job IDTRUEFALSETRUEFALSECount TRUECount FALSE
2886a1092bd7e42b7b8e28a9806ee7186anukritiabhyudanukritiabhyud14734
3a2e94a0e10074d2b948f7c29377b7ddbanukritiabhyudanukritiabhyud14734
4d8d20a671e0e4beeae283a06b2fe90c3anukritiabhyudanukritiabhyud14734
5ef7062a43ead4d4d82f49a010e3261abanukritiabhyudanukritiabhyud14734
64358995e376845dc80f86263fb4d5461agangeleabhyudagangeleabhyud19634
743e34098d347474b9decef6f631dd30cagangeleabhyudagangeleabhyud19634
861247bd1e62842368bf234c7fbb04d7aagangeleabhyudagangeleabhyud19634
99e5ecc4148714d33babd21d6d1cf4f6cagangeleabhyudagangeleabhyud19634
10b40e8792f5f34a0c86e23613376c4ee0agangeleabhyudagangeleabhyud19634
11d143b2500e2e4ebbbb9115df0c25645dagangeleabhyudagangeleabhyud19634
12dc40567391264eecbccfdb5d36115058agangeleabhyudagangeleabhyud19634
13e944726741184e1aa938a405f7fc2f42agangeleabhyudagangeleabhyud19634
14eb91d2961077457d8103f11e33c0e086agangeleabhyudagangeleabhyud19634
15058c1c8233504021abd40de72f1e235awajakhanabhyud29434
1613301447e24047bd8642c8d85f7c3abdwajakhanabhyud29434
171d75802b3f1343b09af8045bbb9a2644wajakhanabhyudwajakhanabhyud29434
181f48ef1eb11449318e78a17e0d48ed7cwajakhanabhyudwajakhanabhyud29434
195169d12ba01d42b9ad12cb45069a8021wajakhanabhyudwajakhanabhyud29434
2055a2d2e633c54366b32a83097581c97dwajakhanabhyudwajakhanabhyud29434
16th (2)


I assumed i should be removing blanks. Removed and put a pivot. Oh boy, these are the results :D:

Allocation test.xlsm
EFGH
2ManagersTrue DataManagersFalse Data
3snghoj320nkakulla272
4biswadm265bjjosh147
5saithom82rdnsh131
6skaveesh52shprakha82
7alfrdav50saithom55
8pnrhul50alfrdav51
9wajakhan50blbi50
10bjjosh50skaveesh50
11agangele50pnrhul50
12shprakha50chatuadi50
Sheet5


Also, people with lowest data are not allocated 100%:

Allocation test.xlsm
EFGH
34estrb45estrb45
35mohaaimr18mohaaimr43
36abhyud5abhyud32
37rogelios3sammula30
38sammula2rogelios3
39Grand Total2142bippakay1
40Grand Total2142
Sheet5
 
Upvote 0
remove apostrophe before
VBA Code:
 E2.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

and blanks are removed
 
Upvote 0
Didn't notice that.
But the output is same as mentioned.

It has created an output like this:

Allocation test.xlsm
ABCDEFG
1Job IDTRUEFALSETRUEFALSECount TRUECount FALSE
2886a1092bd7e42b7b8e28a9806ee7186anukritiabhyudanukritiabhyud14734
3a2e94a0e10074d2b948f7c29377b7ddbanukritiabhyudanukritiabhyud14734
4d8d20a671e0e4beeae283a06b2fe90c3anukritiabhyudanukritiabhyud14734
5ef7062a43ead4d4d82f49a010e3261abanukritiabhyudanukritiabhyud14734
64358995e376845dc80f86263fb4d5461agangeleabhyudagangeleabhyud19634
743e34098d347474b9decef6f631dd30cagangeleabhyudagangeleabhyud19634
861247bd1e62842368bf234c7fbb04d7aagangeleabhyudagangeleabhyud19634
99e5ecc4148714d33babd21d6d1cf4f6cagangeleabhyudagangeleabhyud19634
10b40e8792f5f34a0c86e23613376c4ee0agangeleabhyudagangeleabhyud19634
11d143b2500e2e4ebbbb9115df0c25645dagangeleabhyudagangeleabhyud19634
12dc40567391264eecbccfdb5d36115058agangeleabhyudagangeleabhyud19634
13e944726741184e1aa938a405f7fc2f42agangeleabhyudagangeleabhyud19634
14eb91d2961077457d8103f11e33c0e086agangeleabhyudagangeleabhyud19634
15058c1c8233504021abd40de72f1e235awajakhanabhyud29434
1613301447e24047bd8642c8d85f7c3abdwajakhanabhyud29434
171d75802b3f1343b09af8045bbb9a2644wajakhanabhyudwajakhanabhyud29434
181f48ef1eb11449318e78a17e0d48ed7cwajakhanabhyudwajakhanabhyud29434
195169d12ba01d42b9ad12cb45069a8021wajakhanabhyudwajakhanabhyud29434
2055a2d2e633c54366b32a83097581c97dwajakhanabhyudwajakhanabhyud29434
16th (2)


I assumed i should be removing blanks. Removed and put a pivot. Oh boy, these are the results :D:

Allocation test.xlsm
EFGH
2ManagersTrue DataManagersFalse Data
3snghoj320nkakulla272
4biswadm265bjjosh147
5saithom82rdnsh131
6skaveesh52shprakha82
7alfrdav50saithom55
8pnrhul50alfrdav51
9wajakhan50blbi50
10bjjosh50skaveesh50
11agangele50pnrhul50
12shprakha50chatuadi50
Sheet5


Also, people with lowest data are not allocated 100%:

Allocation test.xlsm
EFGH
34estrb45estrb45
35mohaaimr18mohaaimr43
36abhyud5abhyud32
37rogelios3sammula30
38sammula2rogelios3
39Grand Total2142bippakay1
40Grand Total2142
Sheet5
 
Upvote 0
The code is now working - now I need to teach you how to write rules to get what you want. I will help you to get started

ALL entries are placed in the new sheet - use successive rules to delete more and more of what you want to delete, making sure that you never delete something that you want to retain

RULE 1
people with lowest data are not allocated 100%:

This seems to be very important to you
How do you define "lowest" ?
- eg all alloactions of TRUE & FALSE below 30 should be retained

Tell me in words what RULE1 should be
 
Upvote 0
I will set out the rules which will include RULE 1 as well:
-I receive different sets of data each day to sort out this way. The numbers for each person is different each day but the format is the same. The ones with high and low numbers of True and False might remain on the same levels of data. By levels i mean, high/low numbers of true/false.
-Given that, i need to allocate 60 from each manager's data.
-*RULE 1* Some managers have significantly very fewer data of either True (or) Both True and False (<60). In that case, i need to allocate their whole data which is the only data they have (keeping in mind that the cap is =60).
Ex.:- manager1 has 14 True data in original file and 32 False. All his job ID's should be selected. (14 True and 32 False).
Manager2 has 30 True data and 80 False data. In such case, All of his True data should be selected and False should be = the max. cap (60).
*This is very important because i can't eliminate the ONLY numbers of data he has as everyone else has to allocated at least and =60.
-Now 60 for both True and False might not be statistically achieved given the conditions. In that case, a margin of + 5 or upto +10 (max.) for True and + (or) 5 or upto -10 (max.) False can be accepted which will still make up for significant data.
 
Upvote 0
Post#88 requirement is so different to what was in your first post that I have probably approached this in the wrong manner from the start
- I am going round in circles getting nowhere
- it neeeds a new pair of eyes

I suggest you start a new thread, using the details in post88 as your FIRST post and include sample data perhaps using XL2BB
Thread Title possibly VBA to pick data based on conflictng conditions
Linking back to this thread would be unhelpful to anyone trying to help you

I withdraw from this thread
Good luck
 
Upvote 0
Post#88 requirement is so different to what was in your first post that I have probably approached this in the wrong manner from the start
- I am going round in circles getting nowhere
- it neeeds a new pair of eyes

I suggest you start a new thread, using the details in post88 as your FIRST post and include sample data perhaps using XL2BB
Thread Title possibly VBA to pick data based on conflictng conditions
Linking back to this thread would be unhelpful to anyone trying to help you

I withdraw from this thread
Good luck
Thanks for you patience and help :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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