SOLVED

Sorting list of values by adjacent column text

%3CLINGO-SUB%20id%3D%22lingo-sub-3211863%22%20slang%3D%22en-US%22%3ESorting%20list%20of%20values%20by%20adjacent%20column%20text%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3211863%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20am%20looking%20to%20create%20a%20vba%20macro%20button%20to%20sort%20through%20a%20single%20column%20range%20(in%20the%20example%20image%20it%20would%20be%20column%20%22C%22)%20and%20copy%20corresponding%20adjacent%20cell%20values%20(Column%20%22D%22)%20to%20another%20range%20area.%20The%20range%20area%20that%20the%20cell%20values%20are%20to%20be%20copied%20to%20is%20limited%20to%2023%20cells%2C%20once%20the%2023%20cells%20are%20fill%20the%20macro%20will%20need%20to%20continue%20copying%20the%20cells%20into%20the%20next%20specified%20range.%20These%20areas%20are%20grouped%20into%20pairs%20and%20named%20as%20different%20sheets.%20The%20macro%20will%20need%20to%20stop%20copying%20the%20adjacent%20cells%20onto%20the%20sheet%20when%20the%20text%20in%20column%20%22C%22%20changes.%20Ie%20S8%20and%20S9%20cannot%20be%20on%20the%20same%20sheet%20as%20one%20is%20a%20Wall%20and%20one%20is%20a%20truss%20in%20column%20%22C%22.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22EXAMPLE.xlsx%20-%20Excel%202022-02-25%2007.31.44.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F351117iEF6630204226E51B%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22EXAMPLE.xlsx%20-%20Excel%202022-02-25%2007.31.44.png%22%20alt%3D%22EXAMPLE.xlsx%20-%20Excel%202022-02-25%2007.31.44.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3211863%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3212183%22%20slang%3D%22en-US%22%3ERe%3A%20Sorting%20list%20of%20values%20by%20adjacent%20column%20text%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3212183%22%20slang%3D%22en-US%22%3ECan%20you%20attach%20a%20sample%20file%3F%3CBR%20%2F%3E%3CBR%20%2F%3EHave%20you%20considered%20just%20writing%20a%20formula%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3212235%22%20slang%3D%22en-US%22%3ERe%3A%20Sorting%20list%20of%20values%20by%20adjacent%20column%20text%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3212235%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1316276%22%20target%3D%22_blank%22%3E%40Wildecoyote1966%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20there%2C%3C%2FP%3E%3CP%3EYea%20I%20could%20use%20cell%20formulas%20if%20that%20works.%20The%20cell%20formula%20would%20need%20to%20work%20on%20a%20much%20larger%20scale%20though%20then%20what%20I%20have%20shown%20as%20an%20example.%20That%20was%20why%20I%20was%20thinking%20vba%20macros%20may%20be%20better%20suited%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHopefully%20the%20link%20below%20works%20for%20you%2C%20the%20folder%20contains%20the%20example%20I%20have%20shown%20on%20the%20original%20post.%20I%20cant%20use%20an%20actual%20example%20as%20the%20document%20would%20contain%20company%20IP.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftextormetal.sharepoint.com%2F%3Af%3A%2Fs%2FHytek%2FEjyZ4vmTMQVGoOzGXjzkt9cB2adRt3JiI3HFmnk4jAqclQ%3Fe%3DnohZen%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftextormetal.sharepoint.com%2F%3Af%3A%2Fs%2FHytek%2FEjyZ4vmTMQVGoOzGXjzkt9cB2adRt3JiI3HFmnk4jAqclQ%3Fe%3DnohZen%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3212250%22%20slang%3D%22en-US%22%3ERe%3A%20Sorting%20list%20of%20values%20by%20adjacent%20column%20text%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3212250%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1317257%22%20target%3D%22_blank%22%3E%40camerongreenfield%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHello.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20not%20sure%20what%20the%20selection%20criteria%20is.%20Do%20the%20values%20in%20column%20D%20change.%20so%20far%20I%20just%20went%20and%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DD17%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Edown%20the%20first%20column%20as%20that%20is%2023%20lines%20as%20per%20original%20instructions.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3212289%22%20slang%3D%22en-US%22%3ERe%3A%20Sorting%20list%20of%20values%20by%20adjacent%20column%20text%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3212289%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1316276%22%20target%3D%22_blank%22%3E%40Wildecoyote1966%3C%2FA%3E%3CBR%20%2F%3EYes%20both%20column%20C%20and%20D%20would%20have%20their%20values%20change%20as%20the%20values%20will%20be%20copied%20in%20from%20another%20workbook.%3CBR%20%2F%3EThe%20cell%20values%20down%20column%20C%20represent%20the%20%22group%22%20that%20the%20values%20in%20column%20D%20belong%20to.%20That's%20why%20S8%20and%20S9%20cannot%20be%20on%20the%20same%20sheet%20as%20one%20is%20in%20the%20walls%20%22group%22%20and%20the%20other%20is%20in%20the%20truss%20%22group%22.%20S8%20would%20follow%20S7%20on%20the%20sheet%20list%20(unless%20there%20is%20no%20more%20cells%20for%20that%20sheet%20list%2C%20in%20which%20case%20it%20would%20go%20onto%20the%20next%20sheet).%20S9%20would%20go%20onto%20the%20next%20available%20sheet%20regardless%20as%20it%20is%20the%20start%20of%20a%20new%20%22group%22.%20Hope%20that%20makes%20sense.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3212301%22%20slang%3D%22en-US%22%3ERe%3A%20Sorting%20list%20of%20values%20by%20adjacent%20column%20text%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3212301%22%20slang%3D%22en-US%22%3E%3CP%3Ehi%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1317257%22%20target%3D%22_blank%22%3E%40camerongreenfield%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELike%20I%20said%20the%20column%20G%20is%2023%20lines%20long%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20try%20something%20like%20at%20G5%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DIF(C4%3D%22Walls%22%2CD4%2C%22%22)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3Bat%20J5%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DIF(C4%3D%22Truss%22%2CD4%2C%22%22)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eif%20you%20the%20name%20in%20column%20D%20changes%20say%20from%20Walls%20to%20Truss%20you%20would%20get%20a%20blank%20cell%20in%20that%20line.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi,

I am looking to create a vba macro button to sort through a single column range (in the example image it would be column "C") and copy corresponding adjacent cell values (Column "D") to another range area. The range area that the cell values are to be copied to is limited to 23 cells, once the 23 cells are fill the macro will need to continue copying the cells into the next specified range. These areas are grouped into pairs and named as different sheets. The macro will need to stop copying the adjacent cells onto the sheet when the text in column "C" changes. Ie S8 and S9 cannot be on the same sheet as one is a Wall and one is a truss in column "C".

EXAMPLE.xlsx - Excel 2022-02-25 07.31.44.png

7 Replies
Can you attach a sample file?

Have you considered just writing a formula?

@Wildecoyote1966 

Hi there,

Yea I could use cell formulas if that works. The cell formula would need to work on a much larger scale though then what I have shown as an example. That was why I was thinking vba macros may be better suited?

 

Hopefully the link below works for you, the folder contains the example I have shown on the original post. I cant use an actual example as the document would contain company IP.

 

https://textormetal.sharepoint.com/:f:/s/Hytek/EjyZ4vmTMQVGoOzGXjzkt9cB2adRt3JiI3HFmnk4jAqclQ?e=nohZ...

@camerongreenfield 

 

Hello.

 

I'm not sure what the selection criteria is. Do the values in column D change. so far I just went and

=D17

down the first column as that is 23 lines as per original instructions.

@Wildecoyote1966
Yes both column C and D would have their values change as the values will be copied in from another workbook.
The cell values down column C represent the "group" that the values in column D belong to. That's why S8 and S9 cannot be on the same sheet as one is in the walls "group" and the other is in the truss "group". S8 would follow S7 on the sheet list (unless there is no more cells for that sheet list, in which case it would go onto the next sheet). S9 would go onto the next available sheet regardless as it is the start of a new "group". Hope that makes sense.

hi@camerongreenfield 

 

Like I said the column G is 23 lines long

 

I would try something like at G5

=IF(C4="Walls",D4,"")

 at J5

=IF(C4="Truss",D4,"")

 

if you the name in column D changes say from Walls to Truss you would get a blank cell in that line.

 

best response confirmed by camerongreenfield (Occasional Contributor)
Solution

@camerongreenfield 

Sub sort()

Dim i As Integer
Dim j As Integer
Dim z As Integer
Dim w As Integer

Range("G5:MM27").Clear

w = 7
z = 0
j = 5
For i = 4 To 300

If Cells(i + 1, 3).Value = Cells(i, 3).Value Then

If z = 23 Then

    If w Mod 3 = 1 Then
    w = w + 1
    ElseIf w Mod 3 = 2 Then
    w = w + 2
    End If
    
j = 5
Cells(j, w).Value = Cells(i, 4).Value
j = j + 1
z = 1

Else

Cells(j, w).Value = Cells(i, 4).Value
j = j + 1
z = z + 1

End If

Else

If z = 23 Then

    If w Mod 3 = 1 Then
    w = w + 1
    Else
    End If

j = 5
Cells(j, w).Value = Cells(i, 4).Value

Else

Cells(j, w).Value = Cells(i, 4).Value

    If w Mod 3 = 1 Then
    w = w + 3
    Else
    w = w + 2
    End If
    
j = 5

z = 0

End If

End If

Next i

End Sub

Maybe with these lines of code which seem to work in my spreadsheet. Click the button in cell S2 to start the macro. The macro currently works for entries in range C4:C300 and can easily be adapted to other ranges.

@Quadruple_Pawn
Thank you! Looks to be exactly what I was after