-I receive different sets of data each day to sort and pick rows based on few conflicting conditions.
Here's what the data contains:
Column(A) - Job ID
Column(B) - Managers that have done the job.
Column(C) - Contains whether or not they've done the job (TRUE or FALSE).
*JOB ID:
- EVERY Job ID contains EXACTLY 2 lines
- EVERY Job ID contains one TRUE and one FALSE
- EVERY Job ID contains 2 Names. Although few Job IDs contains same manager for both TRUE and FALSE.
-There are around 35 - 40 managers in total and the data set has nearly 10000 rows of this data.
-Managers have TRUE and FALSE conflict for a job (Column C) and i need to pick few samples to audit these.
Rules for Macro:
- The sample data SHOULD include 60 Job IDs of both TRUE and FALSE selections of a manager.
- When a job ID is picked for manager's "TRUE" sample, the manager's data in "FALSE" column should also be picked. Keeping in mind his max. False data did not breach 60 threshold.
- 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).
Example:- 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.) for False can be accepted which will still make up for significant allocation of data.
What i've tried already:
Problem 1:
This code actually gives satisfying results given all conditions give above are met. But more of True data is allocated for few managers who have more percentage of "TRUE" in the entire data:
Problem 2:
and More of False data significantly (>60) for very few managers whole likely have higher % of "FALSE" in entire data:
-Managers with least numbers in entire data are allocated 100% as mentioned in the above condition:
Only blockers are problem 1 and 2.
If anyone could solve this issue, i'd really appreciate it and would be of great help! Thanks in advance!
Here's what the data contains:
Column(A) - Job ID
Column(B) - Managers that have done the job.
Column(C) - Contains whether or not they've done the job (TRUE or FALSE).
Allocation test.xlsm | |||||
---|---|---|---|---|---|
A | B | C | |||
1 | Job ID | Manager | Dislike Option | ||
2 | 490de5c8827c4392a33b916130c55e60 | snghoj | FALSE | ||
3 | 490de5c8827c4392a33b916130c55e60 | chatamba | TRUE | ||
4 | 8220df0bb10a4a12af3b381656a48d7c | blbi | FALSE | ||
5 | 8220df0bb10a4a12af3b381656a48d7c | chatamba | TRUE | ||
6 | de5e9277b7fc47a691e7400f3a4aa37b | kuniarun | FALSE | ||
7 | de5e9277b7fc47a691e7400f3a4aa37b | blbi | TRUE | ||
8 | e20eeec480404487a5b3f9724a36e41f | abhyud | FALSE | ||
9 | e20eeec480404487a5b3f9724a36e41f | pnrhul | TRUE | ||
10 | b114cfafb45a4a7c9ca24ce04f4d06b8 | snghoj | FALSE | ||
11 | b114cfafb45a4a7c9ca24ce04f4d06b8 | rogelios | TRUE | ||
12 | 60dcb9a3fd804dc9924ee073b47468ed | blbi | FALSE | ||
13 | 60dcb9a3fd804dc9924ee073b47468ed | snghoj | TRUE | ||
14 | 387d3c1bee1145efb5518d9164ae33ba | blbi | FALSE | ||
15 | 387d3c1bee1145efb5518d9164ae33ba | kuniarun | TRUE | ||
20th |
*JOB ID:
- EVERY Job ID contains EXACTLY 2 lines
- EVERY Job ID contains one TRUE and one FALSE
- EVERY Job ID contains 2 Names. Although few Job IDs contains same manager for both TRUE and FALSE.
-There are around 35 - 40 managers in total and the data set has nearly 10000 rows of this data.
-Managers have TRUE and FALSE conflict for a job (Column C) and i need to pick few samples to audit these.
Rules for Macro:
- The sample data SHOULD include 60 Job IDs of both TRUE and FALSE selections of a manager.
- When a job ID is picked for manager's "TRUE" sample, the manager's data in "FALSE" column should also be picked. Keeping in mind his max. False data did not breach 60 threshold.
- 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).
Example:- 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.) for False can be accepted which will still make up for significant allocation of data.
What i've tried already:
VBA Code:
Option Explicit
Dim wf As WorksheetFunction
Dim ID As Range, Mgr As Range, Opt As Range
Dim A2 As Range, B2 As Range, C2 As Range
Dim ws2 As Worksheet, rng As Range, lastR As Long, r As Long
Dim count1 As Long, count2 As Long
Sub AllocateV3()
Application.ScreenUpdating = False
Set wf = Application.WorksheetFunction
'sort original data by job number first
With Sheets("19th")
.Sort.SortFields.Clear
lastR = .Range("A" & Rows.Count).End(xlUp).Row
With .Range("A2:C" & lastR)
.Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlNo
End With
'add sheet
.Copy after:=Sheets(Sheets.Count)
End With
'set ranges in new sheet
Set ws2 = Sheets(Sheets.Count)
With ws2
Set A2 = .Range("A:A")
Set B2 = .Range("B:B")
Set C2 = .Range("C:C")
End With
'loop through values
For r = 2 To lastR Step 2
With ws2
Set ID = .Cells(r, 1)
Set Mgr = .Cells(r, 2)
Set Opt = .Cells(r, 3)
Set rng = .Cells(r, 1).Resize(2, 3)
'ensure both elements of the job satisfy the condition
If ID = ID.Offset(1) Then
If wf.CountIfs(B2, Mgr, C2, Opt) > 60 And wf.CountIfs(B2, Mgr.Offset(1), C2, Opt.Offset(1)) > 60 Then rng.ClearContents
Else
GoTo Problem
End If
End With
Next r
On Error Resume Next 'do not remove this line - code fails if special cells finds nothing
A2.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Exit Sub
Problem: MsgBox "Job " & ID & vbCr & "in row " & r, vbExclamation, "JOB ENTRY MISSING"
End Sub
Problem 1:
This code actually gives satisfying results given all conditions give above are met. But more of True data is allocated for few managers who have more percentage of "TRUE" in the entire data:
Allocation test.xlsm | |||||
---|---|---|---|---|---|
H | I | J | |||
2 | Row Labels | TRUE | FALSE | ||
3 | kmarwvo | 113 | 60 | ||
4 | kuniarun | 103 | 60 | ||
5 | saithom | 75 | 60 | ||
6 | chatamba | 73 | 60 | ||
7 | pnrhul | 63 | 60 | ||
8 | blbi | 62 | 60 | ||
9 | chatuadi | 62 | 60 | ||
10 | moumitad | 62 | 60 | ||
11 | tulap | 60 | 60 | ||
12 | sammula | 60 | 60 | ||
19th (2) |
Problem 2:
and More of False data significantly (>60) for very few managers whole likely have higher % of "FALSE" in entire data:
Allocation test.xlsm | |||||
---|---|---|---|---|---|
H | I | J | |||
3 | bjjosh | 60 | 84 | ||
4 | abhyuday | 60 | 63 | ||
5 | moumitad | 62 | 60 | ||
6 | wskh | 60 | 60 | ||
7 | rdnsh | 60 | 60 | ||
8 | abhyud | 37 | 60 | ||
19th (2) |
-Managers with least numbers in entire data are allocated 100% as mentioned in the above condition:
Allocation test.xlsm | ||||||
---|---|---|---|---|---|---|
H | I | J | K | |||
35 | nikumark | 46 | 60 | 106 | ||
36 | mamidih | 41 | 60 | 101 | ||
37 | abhyud | 37 | 60 | 97 | ||
38 | kalarik | 34 | 60 | 94 | ||
39 | sanikh | 17 | 41 | 58 | ||
19th (2) |
Only blockers are problem 1 and 2.
If anyone could solve this issue, i'd really appreciate it and would be of great help! Thanks in advance!