Sorting list of values by adjacent column text

camerongreenfield

New Member
Joined
Feb 24, 2022
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
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".
 

Attachments

  • EXAMPLE.xlsx - Excel 2022-02-25 07.31.44.png
    EXAMPLE.xlsx - Excel 2022-02-25 07.31.44.png
    92.1 KB · Views: 12

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Cameron, here is my solution. Now I have tried different scenarios to make sure that the program works correctly. One set-up that probably won’t work, if say Wallis has more than 46 items. As long as everybody has less than 46 items it will work. If you need any one sheet to accommodate more than 46 items, let us know. My solutions usually create more questions than answer so let the discussion begin. Sometimes the A plus students weigh in and gives us an even better program. Let’s see.


VBA Code:
Sub CopyPaste1()

Dim col1 As Integer
Dim row1 As Integer
Dim row2 As Integer
Dim CellCnt As Integer

CellCnt = Cells(Rows.Count, "C").End(xlUp).Row

row1 = 4
row2 = 5
col1 = 7
Application.ScreenUpdating = False

Do Until IsEmpty(Cells(row1, 3))
Cells(row1, 4).Select
If Cells(row1, 3) = Cells(row1 + 1, 3) Then
        If col1 Mod 3 = 1 And row2 <= 27 Then
            Cells(row2, col1) = Cells(row1, 4)
            Cells(row2, col1).Select
            row2 = row2 + 1
        ElseIf col1 Mod 3 = 1 And row2 = 28 Then
            col1 = col1 + 1
            row2 = 5
            Cells(row2, col1) = Cells(row1, 4)
            Cells(row2, col1).Select
            row2 = row2 + 1
        ElseIf col1 Mod 3 = 2 Then
            Cells(row2, col1).Select
            Cells(row2, col1) = Cells(row1, 4)
            row2 = row2 + 1
        End If
Else
        If col1 Mod 3 = 1 And row2 <= 27 Then
            Cells(row2, col1).Select
            Cells(row2, col1) = Cells(row1, 4)
            col1 = col1 + 3
            row2 = 5
        ElseIf col1 Mod 3 = 1 And row2 >= 28 Then
            row2 = 5
            col1 = col1 + 1
            Cells(row2, col1) = Cells(row1, 4)
            Cells(row2, col1).Select
            col1 = col1 + 2
        ElseIf col1 Mod 3 = 2 Then
            Cells(row2, col1) = Cells(row1, 4)
            Cells(row2, col1).Select
            col1 = col1 + 2
            row2 = 5
        End If

End If
row1 = row1 + 1
Loop
Application.ScreenUpdating = True
End Sub




22-02-25 copy paste.xlsm
CDEFGHIJKLMN
4WALLSL1SHEET 1SHEET 2SHEET 3
5WALLSL2L1S2S4
6WALLSL3L2S3S5
7WALLSL4L3S6
8WALLSL5L4S7
9WALLSL6L5S8
10WALLSL7L6S9
11WALLSL8L7S10
12WALLSL9L8S11
13WALLSL10L9S12
14WALLSL11L10S13
15WALLSL12L11S14
16WALLSL13L12S15
17WALLSL14L13S16
18WALLSL15L14S17
19WALLSL16L15S18
20WALLSL17L16S19
21WALLSN1L17
22WALLSN2N1
23WALLSN3N2
24WALLSN4N3
25WALLSN5N4
26WALLSS1N5
27WALLSS2S1
28WALLSS3
29TRUSSS4
30TRUSSS5
31TRUSSS6
32TRUSSS7
33TRUSSS8
34TRUSSS9
35TRUSSS10
36TRUSSS11
37TRUSSS12
38TRUSSS13
39TRUSSS14
40TRUSSS15
41TRUSSS16
42TRUSSS17
43TRUSSS18
44TRUSSS19
Copy Pate
 
Upvote 0
Hi Ezguy4u,
Yes that's very close to what I am looking for. However, there may be cases where the category "Walls" for example may be over 46 items and will then need to continue onto the next sheet. If we could have the program do that as well then it would be exactly what I am after. Thanks :)
 
Upvote 0
Cameron, it turns out that I just had to add a couple more lines of code and it now accommodates more than 46 items. Be sure to let us know if there are any problems.



VBA Code:
Sub CopyPaste1revA()

Dim col1 As Integer
Dim row1 As Integer
Dim row2 As Integer
Dim CellCnt As Integer

CellCnt = Cells(Rows.Count, "C").End(xlUp).Row

row1 = 4
row2 = 5
col1 = 7
Application.ScreenUpdating = False

Do Until IsEmpty(Cells(row1, 3))
Cells(row1, 4).Select
If Cells(row1, 3) = Cells(row1 + 1, 3) Then
        If col1 Mod 3 = 1 And row2 <= 27 Then
            Cells(row2, col1) = Cells(row1, 4)
            Cells(row2, col1).Select
            row2 = row2 + 1
        ElseIf col1 Mod 3 = 1 And row2 = 28 Then
            col1 = col1 + 1
            row2 = 5
            Cells(row2, col1) = Cells(row1, 4)
            Cells(row2, col1).Select
            row2 = row2 + 1
        ElseIf col1 Mod 3 = 2 And row2 <= 27 Then
            Cells(row2, col1).Select
            Cells(row2, col1) = Cells(row1, 4)
            row2 = row2 + 1
         ElseIf col1 Mod 3 = 2 And row2 = 28 Then
            col1 = col1 + 2
            row2 = 5
            Cells(row2, col1).Select
            Cells(row2, col1) = Cells(row1, 4)
            row2 = row2 + 1
        End If
Else
        If col1 Mod 3 = 1 And row2 <= 27 Then
            Cells(row2, col1).Select
            Cells(row2, col1) = Cells(row1, 4)
            col1 = col1 + 3
            row2 = 5
        ElseIf col1 Mod 3 = 1 And row2 >= 28 Then
            row2 = 5
            col1 = col1 + 1
            Cells(row2, col1) = Cells(row1, 4)
            Cells(row2, col1).Select
            col1 = col1 + 2
        ElseIf col1 Mod 3 = 2 Then
            Cells(row2, col1) = Cells(row1, 4)
            Cells(row2, col1).Select
            col1 = col1 + 2
            row2 = 5
        End If

End If
row1 = row1 + 1
Loop
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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