Find a subject that is in column "P" in columns "G" and "L" and export.

harzer

Board Regular
Joined
Dec 15, 2021
Messages
122
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,
Given my level in VBA, it is impossible for me to come up with a solution, hence my request to the Experts among you to help me, I would like to ask you for a solution with UBound, or with dictionary, or both combined, because the data to be processed is numerous.
Here is the problem:
I need to go through all the cells in column "P" and see if I find identical cells in column "G" and "L".
We start with the cell in "P2", the value of cell "P2" = "3024-066/2020 M", when the cell in column "G" is equal to the value of cell "P2", we export the value of the cell of column "F" in column "Q" and that of column "H" in column "R" and continues thus until the end of column "G".
When the search in column "G" is completed, we will continue the search in column "L" from "L2" to the last line.
When this step is completed, we will do the same for the cell located in “P3”

Initial sheet:

Classeur1.xlsm
ABCDEFGHIJKLMNOP
1SujetsParentsnombregénérationsSujetsParentsnombregénérationsParents
23024-060/2023 M3024-066/2020 M2G127-009/2023 FCT10-075/2005 M95G413024-066/2020 M
33024-0555/2023 M2207-032/2020 F5G127-010/2023 FSC14-020/2005 F95G52207-032/2020 F
43024-073/2023 M392-019/2018 M9G227-004/2023 F235-096/2005 M70G9392-019/2018 M
55919-089/2023 M3024-066/2020 M15G327-055/2023 F03-001/2010 F26G23024-011/2022 F
65919-001/2023 M3024-011/2022 F1G427-009/2023 F838-017/2008 M26G85617-061/2019 F
75919-002/2023 M5617-061/2019 F50G327-009/2023 FSC14-019/2006 F25G6SC14-020/2005 F
827-010/2021 M3024-066/2020 M48G427-009/2023 F3024-066/2020 M24G14235-096/2005 M
927-013/2021 MSC14-020/2005 F48G1127-009/2023 FSC14-018/2008 F24G6SC14-068/2006 M
1027-010/2021 M235-096/2005 M35G427-009/2023 FSC14-045/2004 M24G3856-062/2009 M
1127-015/2021 MSC14-068/2006 M30G1027-009/2023 FSC14-009/2004 F24G9856-148/2007 M
1227-023/2021 M856-062/2009 M29G427-009/2023 F856-062/2009 M22G803*-156/2011 M
1327-077/2021 M856-148/2007 M28G2027-009/2023 F856-131/2011 F19G1856-090/2010 M
1427-055/2021 M03*-156/2011 M18G527-009/2023 F3024-066/2020 M19G18CT10-075/2005 M
1527-041/2021 M856-090/2010 M18G627-009/2023 F856-071/2007 F19G3003-001/2010 F
1627-010/2021 M2207-032/2020 F18G1027-009/2023 F3024-066/2020 M16G9838-017/2008 M
1727-009/2023 F2207-032/2020 F55G13SC14-019/2006 F
18SC14-018/2008 F
19SC14-045/2004 M
20SC14-009/2004 F
21856-131/2011 F
22856-071/2007 F
Anc_Communs


Unless I'm mistaken, here is the sheet with the desired result (For the first subject which is in cell "P2").

Classeur1.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1SujetsParentsnombregénérationsSujetsParentsnombregénérationsParents
23024-060/2023 M3024-066/2020 M2G127-009/2023 FCT10-075/2005 M95G413024-066/2020 M3024-060/2023 M25919-089/2023 M1527-010/2021 M4827-009/2023 F2427-009/2023 F1927-009/2023 F16
33024-0555/2023 M2207-032/2020 F5G127-010/2023 FSC14-020/2005 F95G52207-032/2020 F
43024-073/2023 M392-019/2018 M9G227-004/2023 F235-096/2005 M70G9392-019/2018 M
55919-089/2023 M3024-066/2020 M15G327-055/2023 F03-001/2010 F26G23024-011/2022 F
65919-001/2023 M3024-011/2022 F1G427-009/2023 F838-017/2008 M26G85617-061/2019 F
75919-002/2023 M5617-061/2019 F50G327-009/2023 FSC14-019/2006 F25G6SC14-020/2005 F
827-010/2021 M3024-066/2020 M48G427-009/2023 F3024-066/2020 M24G14235-096/2005 M
927-013/2021 MSC14-020/2005 F48G1127-009/2023 FSC14-018/2008 F24G6SC14-068/2006 M
1027-010/2021 M235-096/2005 M35G427-009/2023 FSC14-045/2004 M24G3856-062/2009 M
1127-015/2021 MSC14-068/2006 M30G1027-009/2023 FSC14-009/2004 F24G9856-148/2007 M
1227-023/2021 M856-062/2009 M29G427-009/2023 F856-062/2009 M22G803*-156/2011 M
1327-077/2021 M856-148/2007 M28G2027-009/2023 F856-131/2011 F19G1856-090/2010 M
1427-055/2021 M03*-156/2011 M18G527-009/2023 F3024-066/2020 M19G18CT10-075/2005 M
1527-041/2021 M856-090/2010 M18G627-009/2023 F856-071/2007 F19G3003-001/2010 F
1627-010/2021 M2207-032/2020 F18G1027-009/2023 F3024-066/2020 M16G9838-017/2008 M
1727-009/2023 F2207-032/2020 F55G13SC14-019/2006 F
18SC14-018/2008 F
19SC14-045/2004 M
20SC14-009/2004 F
21856-131/2011 F
22856-071/2007 F
Résultat


I remain at your disposal for further information.
I thank you in advance for your suggestions.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
See if this works for you:
VBA Code:
Sub harzer()

Dim LR, LR1, LR2, LC, A, B, C As Long
Dim Chk As String

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.DisplayAlerts = False

LR = Cells(Rows.count, "P").End(xlUp).Row
LR1 = Cells(Rows.count, "G").End(xlUp).Row
LR2 = Cells(Rows.count, "L").End(xlUp).Row

For A = 2 To LR
    Chk = Range("P" & A).Value
    For B = 2 To LR1
        If Range("G" & B).Value = Chk Then
            LC = Cells(A, Columns.count).End(xlToLeft).Column + 1
            Cells(A, LC).Value = Range("F" & B).Value
            Cells(A, LC + 1).Value = Range("H" & B).Value
            Else
        End If
    Next B
    For C = 2 To LR2
        If Range("L" & C).Value = Chk Then
            LC = Cells(A, Columns.count).End(xlToLeft).Column + 1
            Cells(A, LC).Value = Range("K" & C).Value
            Cells(A, LC + 1).Value = Range("M" & C).Value
            Else
        End If
    Next C
Next A

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Solution
Hello dermie_72,
Thanks for your code.
I just initialized the "Lc" variable twice by writing Lc = 1 so that the program gives me the desired result.
I tested your code with my data, it works fine and gives me the desired result, however, it takes a while to run, hence my request to use arrays (UBound) as I know they are very fast.
I would be happy if you know how to adapt your code using arrays (UBound).
Greetings.

VBA Code:
Sub harzer()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.DisplayAlerts = False


lr = Cells(Rows.count, "P").End(xlUp).row
LR1 = Cells(Rows.count, "G").End(xlUp).row
LR2 = Cells(Rows.count, "L").End(xlUp).row

For a = 2 To lr
lc = 1                        'I added this line to make the code work the way I want.                                
    Chk = Range("P" & a).Value
    For b = 2 To LR1
        If Range("G" & b).Value = Chk Then
            lc = Cells(a, Columns.count).End(xlToLeft).Column + 1
            Cells(a, lc).Value = Range("F" & b).Value
            Cells(a, lc + 1).Value = Range("H" & b).Value
            Else
        End If
    Next b
    
lc = 1                        'I added this line to make the code work the way I want.
    For c = 2 To LR2
        If Range("L" & c).Value = Chk Then
            lc = Cells(a, Columns.count).End(xlToLeft).Column + 1
            Cells(a, lc).Value = Range("K" & c).Value
            Cells(a, lc + 1).Value = Range("M" & c).Value
            Else
        End If
    Next c
Next a

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.DisplayAlerts = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,988
Members
449,093
Latest member
Mr Hughes

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