Macro is required to the data filter

harinsh

Active Member
Joined
Feb 7, 2012
Messages
273
Hi Excel Experts,

I am looking for one small automation macro to filter the data below and need to keep one set of data from the input to the output.

We have three columns below and Col-1 where "Y" is applicable in Col-3 then Col-1 cell value should be base and corresponding Col-2 data should copy against the value. Each corresponding value should paste in the different table (refer the output).

Table 1- Input
Col-1Col-2Col-3
1000110011Y
1001110012
1000310013Y
1001210014
1000510015Y
1001410016
1000710017Y
1001610018
1000910019Y
1001810020


Table 2- Output
Output
Col-1Col-2
1000110001
1000110011
1000110012
1000110014
1000510015
1000510016
1000510018
1000510020

Let me know if you need further clarification.

Thank you,
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Let me know if you need further clarification.
I don't see how you got the Output table from that Input. Can you explain step-by-step how you got each row in the Output?


BTW, I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Here is the step by step

1- First filter "Y" in col-3 and take the col-1 cell value (i.e 10001)
2- Paste the above cell value in col-2 and take the value (10011)
3- Paste both above values in empty cells one beside one (10001 - 10011 - Y )

The filter should continue until getting the value incase no value filter should stop and proceed to the second set of data based on value "Y" in Col-3.
 
Upvote 0
Perhaps I am missing something obvious, but I cannot see it.
 
Upvote 0
Here is the step by step

1- First filter "Y" in col-3 and take the col-1 cell value (i.e 10001)
2- Paste the above cell value in col-2 and take the value (10011)
3- Paste both above values in empty cells one beside one (10001 - 10011 - Y )

The filter should continue until getting the value in case no value filter should stop and proceed to the second set of data based on value "Y" in Col-3.

Do you need more clarifications?
 
Upvote 0
That is the same description that you gave before. It makes no sense to me.
Below I have filtered "Y" in col 3.
Why are there four "10001" values in column 1 of the Output?
Where do 10012 and 10014 come from in cells B20 and B21? They seem to have nothing to do with either 10001 in the Input and they also have nothing to do with Y in the Input.

In the Input, the second Y value is for 10003 and 10013. Neither of those values appear in your Output. I don't understand that.

Where do 10016, 10018 and 10020 in cells B23:B26? They seem to have nothing to do with either 10005 in the Input and they also have nothing to do with Y in the Input.

In the Input, the fourth Y value is for 10007 and 10017. Neither of those values appear in your Output. I don't understand that.

In the Input, the fifth Y value is for 10009 and 10019. Neither of those values appear in your Output. I don't understand that.

harnish.xlsm
ABC
1Table 1- Input
2Col-1Col-2Col-3
31000110011Y
51000310013Y
71000510015Y
91000710017Y
111000910019Y
13
14
15Table 2- Output
16Output
17Col-1Col-2
181000110001
191000110011
201000110012
211000110014
22
231000510015
241000510016
251000510018
261000510020
Sheet1
 
Upvote 0
Need to filter the col-3 value "Y" and take the col-1 value and taking the col-2 value and keeping the values one beside another and take the col-2 value again filtering in col-1 and past beside to the next value.
 
Upvote 0
Below I have filtered "Y" in col 3.

Why are there four "10001" values in column 1 of the Output?
Each "Y" value we call as one set and corresponding hierarchy need to be arranged one after another

Where do 10012 and 10014 come from in cells B20 and B21? They seem to have nothing to do with either 10001 in the Input and they also have nothing to do with Y in the Input.
if you filter 10011 then you get 10012 and if you filter 10012 you will get 10014 and so on..., Input "Y" only to take the set as explained above
1671970136050.png


In the Input, the second Y value is for 10003 and 10013. Neither of those values appear in your Output. I don't understand that.
Each "Y" value we call as one set and corresponding hierarchy need to be arranged one after another. If you filter 10003 there is no value so this set end with below output
1671969973813.png


Where do 10016, 10018 and 10020 in cells B23:B26? They seem to have nothing to do with either 10005 in the Input and they also have nothing to do with Y in the Input.
Its hierachy need to align one after one
1671970229081.png


In the Input, the fourth Y value is for 10007 and 10017. Neither of those values appear in your Output. I don't understand that.
Same as above explained if not further data just make it as one set without hiearchy
1671970093168.png


In the Input, the fifth Y value is for 10009 and 10019. Neither of those values appear in your Output. I don't understand that.
Same as above explained if not further data just make it as one set without hiearchy
1671970115033.png


Corrected output
1671969823065.png


My apologies if any confustion.
 

Attachments

  • 1671970089554.png
    1671970089554.png
    2.7 KB · Views: 8
Upvote 0
OK, no wonder I couldn't understand the (incorrect) original sample and results! ;)
Now I understand.

Try this with a copy of your workbook.

VBA Code:
Sub Hierarchy()
  Dim d As Object
  Dim a As Variant, b As Variant, Colm1 As Variant, Colm2 As Variant
  Dim i As Long, j As Long, k As Long
  
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("B2").End(xlDown).Offset(, 1)).Value
  For i = 1 To UBound(a)
    d(a(i, 1)) = a(i, 2)
  Next i
  ReDim b(1 To Rows.Count, 1 To 2)
  For i = 1 To UBound(a)
    If a(i, 3) = "Y" Then
      Colm1 = a(i, 1)
      Colm2 = a(i, 2)
      If d.exists(Colm2) Then
        k = k + 1
        Do
          b(k, 1) = Colm1
          b(k, 2) = Colm2
          k = k + 1
          Colm2 = d(Colm2)
        Loop Until IsEmpty(Colm2)
      End If
    End If
  Next i
  If k > 0 Then
    With Range("A1").End(xlDown).Offset(4)
      .Resize(k, 2).Value = b
      .Offset(-1).Resize(, 2).Value = Range("A1:B1").Value
    End With
  End If
End Sub

Sheet before code:

harnish.xlsm
ABC
1Col-1Col-2Col-3
21000110011Y
31001110012
41000310013Y
51001210014
61000510015Y
71001510016
81000710017Y
91001610018
101000910019Y
111001810020
12
13
14
15
16
17
18
19
20
Sheet1


After code:

harnish.xlsm
ABC
1Col-1Col-2Col-3
21000110011Y
31001110012
41000310013Y
51001210014
61000510015Y
71001510016
81000710017Y
91001610018
101000910019Y
111001810020
12
13
14Col-1Col-2
151000110011
161000110012
171000110014
18
191000510015
201000510016
211000510018
221000510020
23
Sheet1



❗Could you also please address this point I made earlier?
BTW, I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Wonderfully, you have done what exactly I was looking for only one request, is it possible to add even no hierarchy values as well something like this as one set? So, it will be clear that this value does not have any hierarchy.

1671970093168-png.81528
 
Upvote 0

Forum statistics

Threads
1,223,761
Messages
6,174,347
Members
452,556
Latest member
Chrisolowolafe

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