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,
 
, 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.
Sure, it just requires the removal of two lines of the previous code.

Rich (BB code):
Sub Hierarchy_v2()
  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
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
It works very well only the last change I won't disturb anymore and your help highly appreciate and very helpful.

I want to keep same col-1 value should also keep at first level as like below highlighted what to be included in the code, sorry this was last moment change.

1672035738688.png
 
Upvote 0
Do you want this added to the first code (post #9) or the second code (post #11)?
 
Upvote 0
this needs to be added to the same #11.
In that case it sounds like you want this result (green cells to be added), not the one you showed in post #12?

harnish.xlsm
ABC
1Col-1Col-2Col-3
21000110011Y
31001110012
41000310013Y
51001210014
61000510015Y
71001510016
81000710017Y
91001610018
101000910019Y
111001810020
12
13
14Col-1Col-2
151000110001
161000110011
171000110012
181000110014
19
201000310003
211000310013
22
231000510005
241000510015
251000510016
261000510018
271000510020
28
291000710007
301000710017
31
321000910009
331000910019
Sheet1 (2)
 
Upvote 0
that's correct your output
  1. It is important that you give correct input/output in future. Multiple times in this thread you have given incorrect examples. Helpers will soon tire of helping you if they waste their time trying to do what you have asked, only to learn that you have not asked for the correct thing. ;)

  2. Also, I have already asked twice in this thread (now three times) for you to update your account details to include what Excel version(s) you are using as that can sometimes make a difference to the best way to do something.
 
Upvote 0
VBA Code:
Sub Hierarchy_v3()
  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
      k = k + 1
      b(k, 1) = a(i, 1)
      b(k, 2) = a(i, 1)
      Colm1 = a(i, 1)
      Colm2 = a(i, 2)
        k = k + 1
        Do
          b(k, 1) = Colm1
          b(k, 2) = Colm2
          k = k + 1
          Colm2 = d(Colm2)
        Loop Until IsEmpty(Colm2)
    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
 
Upvote 0
You're welcome.
(Pity we didn't ever find out what version you are using ;))
 
Upvote 0

Forum statistics

Threads
1,223,762
Messages
6,174,357
Members
452,558
Latest member
jswan83

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