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
 
Example:

Allocation test.xlsm
ABC
1Job IDTRUEFALSE
2000064c8e46c47cb9979dcc0268f223dmmmadhrbjjosh
30002cfccbfb2485cb46ca73a465f76f6biswadmrdnsh
400110102441d4b478512d5e7691249ddsattilisattili
5001bce89bdba412a9120e6d86346509emtshahsshaka
6001df3620e524224b4cd43d6575f8a63biswadmelisaty
70025cfb502dc4ff1833ce4ee36855055agangelewajakhan
8002646548b2e4428aa059dbacbc7dc92biswadmnikumark
9002701827ff64d5a99c03e8794b481d1snghojalfrdav
100029b3cdad244e6e9489f6cdcb4ccdd7snghojsnghoj
110029c74095cb4c5f848beb7bf158f630mtshahskaveesh
120042544166e14942b4526401ba60dfcfrdnshsiaditi
1300434abee4e84c2f9785d6da6e149211nkakullammmadhr
1400494aee7a0a40e1b7c8ae4c376491c5vnchannkakulla
15004d143185fd4fceb8ef53eb9dca1384saithomsattili
1600544b4cc26f4a05995232dfeb9e5f63mnallanbiswadm
17005518a198e84fb788d98cf649953b94mtshahnkakulla
18005abccbd39442c584d3899d13566326nikumarksnghoj
19005b7f3c92454608806ce1e0b57b9b7awajakhanwajakhan
Sheet2
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Perfect answer
My next post will include data conversion procedure for you to test
 
Upvote 0
Put this in a new module and run Master to test conversion

VBA Code:
Option Explicit

Dim wf As WorksheetFunction
Dim ID As Range, mT As Range, mF 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

Sub Master()
    Application.ScreenUpdating = False
    CreateNewLayout
End Sub

Private Sub CreateNewLayout()
    Set wf = Application.WorksheetFunction
'copy to new sheet and sort
    Sheets("Sheet1").Copy after:=Sheets(Sheets.Count)
    Set ws2 = Sheets(Sheets.Count)
    With ws2
        lastR = .Range("A" & .Rows.Count).End(xlUp).Row
        With .Range("A2:C" & lastR)
            .Sort Key1:=.Range("C2"), Order1:=xlDescending, Header:=xlNo
            .Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlNo
        End With
    End With
'new layout
    With ws2
        .Range("B3:B" & lastR).Copy .Range("D2")
        .Range("A1:A" & lastR).AutoFilter Field:=3, Criteria1:="FALSE"
        .Range("A2:A" & lastR + 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .Range("D1") = "False"
        .Range("B1") = "True"
        .Range("C1").EntireColumn.Delete
    End With
'set ranges
    With ws2
        Set A2 = .Range("A:A")
        Set B2 = .Range("B:B")
        Set C2 = .Range("A:B")
    End With
End Sub
 
Upvote 0
No, there's actually some problem.

-It's giving this error "Identifier under cursor not recognized" for this line:

VBA Code:
.Range("A1:A" & lastR).AutoFilter Field:=3, Criteria1:="FALSE"

It's printing the output in the other sheet this way:

Allocation test.xlsm
ABCD
1Job IDManagerDislike Option
2000064c8e46c47cb9979dcc0268f223dmmmadhrTRUEbjjosh
3000064c8e46c47cb9979dcc0268f223dbjjoshFALSEbiswadm
40002cfccbfb2485cb46ca73a465f76f6biswadmTRUErdnsh
50002cfccbfb2485cb46ca73a465f76f6rdnshFALSEsattili
600110102441d4b478512d5e7691249ddsattiliTRUEsattili
700110102441d4b478512d5e7691249ddsattiliFALSEmtshah
8001bce89bdba412a9120e6d86346509emtshahTRUEsshaka
9001bce89bdba412a9120e6d86346509esshakaFALSEbiswadm
10001df3620e524224b4cd43d6575f8a63biswadmTRUEelisaty
11001df3620e524224b4cd43d6575f8a63elisatyFALSEagangele
Sheet1 (2)


This is the end of the sheet in the new output:

Allocation test.xlsm
ABCD
19969fff8458cdf2d405a813dc693d2441dcctulapFALSEsshaka
19970fffcfac678cc409da81821f2035f719esshakaTRUEnkakulla
19971fffcfac678cc409da81821f2035f719enkakullaFALSEmnallan
19972fffeb549300244e7a619baed7fcacc99mnallanTRUEsshaka
19973fffeb549300244e7a619baed7fcacc99sshakaFALSE
Sheet1 (2)
 
Upvote 0
Try changing
.Range("A1:A" & lastR).AutoFilter Field:=3, Criteria1:="FALSE"
To
.Range("A1:A" & lastR).AutoFilter Field:=3, Criteria1:=FALSE
 
Upvote 0
Still returning the same error unfortunately
Also, D2 needs to be defined right?

VBA Code:
.Range("B3:B" & lastR).Copy .Range("D2")
 
Upvote 0
This sorting VBA can be addressed later, i guess. For now i'll use few formulas and paste the data in the order you suggested. I'm curious to try the latest code for allocation.
 
Upvote 0
-It's giving this error "Identifier under cursor not recognized"
Ah - worked it out
Data filter was already enabled on my sheet1 - so that was copied across when sheet copied - oops!

Insert this
.Range("A1").AutoFilter

Above
.Range("A1:A" & lastR).AutoFilter Field:=3, Criteria1:=FALSE

Also, D2 needs to be defined right?
- the dot( . ) does that
- the code is correct
 
Upvote 0
Perfect! Cross - Checked with real data too.

Allocation test.xlsm
ABC
1Job IDTRUEFALSE
2000064c8e46c47cb9979dcc0268f223dmmmadhrbjjosh
30002cfccbfb2485cb46ca73a465f76f6biswadmrdnsh
400110102441d4b478512d5e7691249ddsattilisattili
5001bce89bdba412a9120e6d86346509emtshahsshaka
6001df3620e524224b4cd43d6575f8a63biswadmelisaty
70025cfb502dc4ff1833ce4ee36855055agangelewajakhan
8002646548b2e4428aa059dbacbc7dc92biswadmnikumark
9002701827ff64d5a99c03e8794b481d1snghojalfrdav
100029b3cdad244e6e9489f6cdcb4ccdd7snghojsnghoj
Sheet1 (2)
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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