Finding a group of cells, copying data from those rows onto another sheet...

mlarson

Well-known Member
Joined
Aug 25, 2011
Messages
509
Office Version
  1. 2010
Platform
  1. Windows
Hello, I would like to find a code for the following. Thanks in advance for any help you can provide!

-Find the first “YesA,” “YesB,” or “YesC” in a given column on “Sheet1.”

-I would like to find one of these firsts in column A, one first in column B, etc. all the way to column L (so 12 columns).

-Permutations/combinations for all 3 possibilities (“YesA,” “YesB,” or “YesC”) across 12 columns (A-L). So, 3^12 = 531,441 groups. See example below.

-For group 1, when a first is found in column A of “Sheet1” I’d like to copy the data in cells O-Z from that same row to “Sheet2” cells H2-S2. When a first is found in column B copy data in cells O-Z to “Sheet2” cells U2-AF2. And a first in column C copy data from O-Z to “Sheet2” cells AH2-AS2… same idea through column L.

-For group 2, when a first is found in column A of “Sheet1” I’d like to copy the data in cells O-Z from that same row to “Sheet2” cells H3-S3. When a first is found in column B copy data in cells O-Z to “Sheet2” cells U3-AF3. And a first in column C copy data from O-Z to “Sheet2” cells AH3-AS3… same idea through column L.

-Group 3 data will go in row 4 of “Sheet2,” group 4 data will go in row 5 of “Sheet2,” etc. all the way through Group 531,441.

-Results will show 531,441 rows of data on “Sheet2.”

Examples of permutations/combinations for each group…
ColA ColB ColC ColD ColE ColF ColG ColH ColI COlJ ColK ColL
Group 1: YesA YesA YesA YesA YesA YesA YesA YesA YesA YesA YesA YesA
Group 2: YesA YesA YesA YesA YesA YesA YesA YesA YesA YesA YesA YesB
Group 3: YesA YesA YesA YesA YesA YesA YesA YesA YesA YesA YesA YesC
Group 4: YesA YesA YesA YesA YesA YesA YesA YesA YesA YesA YesB YesA
Group 5: YesA YesA YesA YesA YesA YesA YesA YesA YesA YesA YesC YesA
Group 6: YesA YesA YesA YesA YesA YesA YesA YesA YesA YesB YesA YesA

Grp 531,441: YesC YesC YesC YesC YesC YesC YesC YesC YesC YesC YesC YesC


Thank you for your help!
 
Last edited:
Imagine this is column A with the rows...

1 no
2 no
3 no
4 no
5 yesB
6 no
7 yesC
8 no
9 no
10 yesA
11 yesA
12 yesC
13 no
14 yesB
15 no

If I'm searching for the first YesA that would be row 10.
If I'm searching for the first YesB that would be row 5.
If I'm searching for the first YesC that would be row 7.

Imagine the following is column B...
1 no
2 yesA
3 yesC
4 no
5 no
6 no
7 yesC
8 yesC
9 no
10 yesB
11 yesA
12 yesC
13 no
14 yesC
15 yesA

First yesA is row 2
First yesB is row 10
First yesC is row 3

So, if a group asks for the first YesA in column A, and the first YesC in column B, then I would be copying data from rows 10 and 3 of Sheet1. The data from those rows to be copied would be cells O10:Z10 and O3:Z3, respectively. That data would be pasted on Sheet2 in cells U2-AF2 and AH2-AS2.

Note: I only included 2 of the 12 columns in the above example to try to convey the concept.
 
Last edited:
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Thanks
You want the results for "YesA", "YesB" and "YesC" all in the one sheet2, suggesting that you are trying to find the first occurrence in each column of any of the three values: YesA or YesB or YesC (i.e. rather than three separate sets of searches for YesA, YesB and YesC individually)?

I'm trying the find the occurrence of a specific text in the cell. All cells will have either "no" "yesA" "yesB" or "yesC", but I'm only interested in finding the specific text. That specific text for a column changes based on which group we're working on. If I'm trying to find "yesA" then I don't care at all about "no" "yesB" or "yesC." If I'm trying to find "yesB" then I don't care about finding "no" "yesA" or "yesC."

I hope I'm making sense and things are becoming clearer.
 
Upvote 0
Ahh, yes. Clear now thanks.

I had assumed that the permutations listed in the 531,441 groups were in columns A-L of Sheet1. I hadn't realised you had other random Yes/No data in Sheet1, and presumably the permutation groups are listed in columns A-l of Sheet2.

Do the permutations need to be in any particular order? Do you have a formula to generate, or do you need one?
 
Upvote 0
Ahh, yes. Clear now thanks.

Excellent!

I had assumed that the permutations listed in the 531,441 groups were in columns A-L of Sheet1. I hadn't realised you had other random Yes/No data in Sheet1, and presumably the permutation groups are listed in columns A-l of Sheet2.

I should have been more clear. The permutations aren't listed anywhere, and I don't really need a list showing the different permutations, but what I need are the results of cells O:Z for each permutation group. With that said, perhaps it's a simpler task if the permutations are listed somewhere in Sheet1 or Sheet2?

Do the permutations need to be in any particular order? Do you have a formula to generate, or do you need one?

No particular order needed.
 
Upvote 0
Here's a file implementing the layout and code below: https://app.box.com/s/rxaorw1m2me9gp6t782trqcil1cvvkal

Dummy data:

Data1: =Sheet1!$A$2:$L$20
Data2: =Sheet1!$O$2:$Z$20


Excel 2010
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1Data1Data2
2NoNoNoYesANoNoNoNoNoNoNoNoO1P1Q1R1S1T1U1V1W1X1Y1Z1
3NoNoNoNoNoNoNoNoNoNoNoNoO2P2Q2R2S2T2U2V2W2X2Y2Z2
4NoNoNoNoNoNoNoNoNoYesANoNoO3P3Q3R3S3T3U3V3W3X3Y3Z3
5NoNoYesCNoYesBYesCNoNoNoNoNoNoO4P4Q4R4S4T4U4V4W4X4Y4Z4
6NoNoNoNoNoNoNoNoNoNoNoNoO5P5Q5R5S5T5U5V5W5X5Y5Z5
7NoNoNoYesCYesCYesBNoNoYesCNoYesBYesCO6P6Q6R6S6T6U6V6W6X6Y6Z6
8YesANoYesANoNoNoNoNoNoNoNoNoO7P7Q7R7S7T7U7V7W7X7Y7Z7
9NoNoNoNoNoNoNoNoNoYesCYesCYesBO8P8Q8R8S8T8U8V8W8X8Y8Z8
10NoNoNoYesBYesANoYesANoYesANoNoNoO9P9Q9R9S9T9U9V9W9X9Y9Z9
11NoYesBNoNoNoNoNoNoNoNoNoNoO10P10Q10R10S10T10U10V10W10X10Y10Z10
12NoYesAYesBNoYesANoNoNoNoYesBYesANoO11P11Q11R11S11T11U11V11W11X11Y11Z11
13YesBNoNoNoNoNoNoYesBNoNoNoNoO12P12Q12R12S12T12U12V12W12X12Y12Z12
14NoNoNoNoNoYesANoYesAYesBNoYesANoO13P13Q13R13S13T13U13V13W13X13Y13Z13
15NoYesCNoNoNoNoYesBNoNoNoNoNoO14P14Q14R14S14T14U14V14W14X14Y14Z14
16NoNoNoNoNoNoNoNoNoNoNoNoO15P15Q15R15S15T15U15V15W15X15Y15Z15
17NoNoNoNoNoNoNoYesCNoNoNoNoO16P16Q16R16S16T16U16V16W16X16Y16Z16
18YesCNoNoNoNoNoNoNoNoNoNoNoO17P17Q17R17S17T17U17V17W17X17Y17Z17
19NoNoNoNoNoNoNoNoNoNoNoYesAO18P18Q18R18S18T18U18V18W18X18Y18Z18
20NoNoNoNoNoNoYesCNoNoNoNoNoO19P19Q19R19S19T19U19V19W19X19Y19Z19
Sheet1


Sheet2 Results (cut down for illustration purposes)

MyList: =Sheet2!$B$1:$B$3
StartHere: =Sheet2!$B$12
Firsts: =Sheet2!$B$6:$M$8
Helper Cells B6 etc: =IFERROR(MATCH($A6,INDEX(Data1,,COLUMN()-COLUMN($B6)+1),),0)
 
Last edited:
Upvote 0

Excel 2010
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBS
1MyListYesA
2YesB
3YesC
4
5Firsts
6YesA71171913913931118
7YesB1210119461412131168
8YesC1714466419166886
9
10Permutations
11(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)(12)
12111111111111O7P7Q7R7S7T7U7V7W7X7Y7Z7O11P11Q11R11S11T11U11V11W11X11Y11Z11O7P7Q7R7S7T7U7V7W7X7Y7Z7O1P1Q1R1S1T1U1V1W1X1Y1Z1
13111111111112O7P7Q7R7S7T7U7V7W7X7Y7Z7O11P11Q11R11S11T11U11V11W11X11Y11Z11O7P7Q7R7S7T7U7V7W7X7Y7Z7O1P1Q1R1S1T1U1V1W1X1Y1Z1
14111111111113O7P7Q7R7S7T7U7V7W7X7Y7Z7O11P11Q11R11S11T11U11V11W11X11Y11Z11O7P7Q7R7S7T7U7V7W7X7Y7Z7O1P1Q1R1S1T1U1V1W1X1Y1Z1
15111111111121O7P7Q7R7S7T7U7V7W7X7Y7Z7O11P11Q11R11S11T11U11V11W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T11U11V11W11X11Y11Z11O7P7Q7R7S7T7U7V7W7X7Y7Z7O1P1Q1R1S1T1U1V1W1X1Y1Z1
26. etc
27112113112223O7P7Q7R7S7T7U7V7W7X7Y7Z7O11P11Q11R11S11T11U11V11W11X11Y11Z11O11P11Q11R11S11T11U11V11W11X11Y11Z11O1P1Q1R1S1T1U1V1W1X1Y1Z1
28. etc
29113223231321O7P7Q7R7S7T7U7V7W7X7Y7Z7O11P11Q11R11S11T11U11V11W11X11Y11Z11O4P4Q4R4S4T4U4V4W4X4Y4Z4O9P9Q9R9S9T9U9V9W9X9Y9Z9
Sheet2
 
Upvote 0
I have shown the permuations as numeric 1,2,3 rather than string YesA, YesB and YesC to cut down memory usage.

Code:
Sub Test()

    Dim vData As Variant, vOut As Variant, vFirsts As Variant, vList As Variant
    Dim lPermutations() As Long
    Dim lStartRow As Long, lNoRows As Long, lPermutationNo As Long, lRowNo As Long, N As Long, i As Long, j As Long, k As Long
    Const NO_COLS = 12
    
    vData = Range("Data2").Value2
    vFirsts = Range("Firsts").Value2
    vList = Range("MyList").Value2
    N = UBound(vList)
    lStartRow = Range("StartHere").Row
    lNoRows = N ^ NO_COLS
    ReDim lPermutations(1 To lNoRows, 1 To NO_COLS)
    Application.ScreenUpdating = False
    Range(Range("StartHere"), Range("StartHere").End(xlDown)).EntireRow.ClearContents
    
    For i = 1 To NO_COLS
        ReDim vOut(1 To lNoRows, 1 To NO_COLS)
        For j = 1 To lNoRows
            lPermutationNo = Int((j - 1) / N ^ (NO_COLS - i)) Mod N + 1
            lPermutations(j, i) = lPermutationNo
            lRowNo = vFirsts(lPermutationNo, i)
            For k = 1 To NO_COLS
                If lRowNo Then
                    vOut(j, k) = vData(lRowNo, k)
                Else
                    vOut(j, k) = "N/A"
                End If
            Next k
        Next j
        Worksheets("Sheet2").Range("U" & lStartRow).Offset(, (NO_COLS+1) * (i - 1)).Resize(lNoRows, NO_COLS).Value = vOut
    Next i
    
    Range("StartHere").Resize(lNoRows, NO_COLS).Value = lPermutations
    Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
Thanks for that! Sheet one has data in about 600 rows. Do I need to expand the range in the code or on the sheet somehow for Data1 and Data2? For example, right now the Dummy data is
Data1: =Sheet1!$A$2:$L$20
Data2: =Sheet1!$O$2:$Z$20
 
Upvote 0
Yes, probably easiest if you just adjust the range names in Excel, changing 20 to the last row of your data.

Data1: =Sheet1!$A$2:$L$20
Data2: =Sheet1!$O$2:$Z$20

The code automatically picks up the entire Data2 range as it is defined in Excel.
 
Upvote 0
Holy canoli, I think it works! I will run some more tests tomorrow morning and keep you posted. Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,456
Messages
6,124,939
Members
449,197
Latest member
k_bs

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