Help with looping autofilter macro to copy and paste data

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Hello guys once again, I'm trying to work on a macro that helps me accomplish the following.

Book1
ABCDEFGHI
1CityNameOrdersOrder Numbers
2New YorkJames A01- James A1111
3New YorkJames B02- James A1115
4New YorkJames C03- James A1117
5New YorkJames D04- James B2550
6New YorkJames E05- James B2555
7New YorkJames F06- James B2560
8New YorkJames G07- James C127
9New YorkJames H08- James C128
10New YorkJames I09- James C129
11
12
Sheet2


I want for this macro to search starting with range("B2") and to look at column G in order to locate all the cells that contain the name in this case "James A" and to then copy the cells in Columns H that contain that value in order to copy and transpose so the final product looks like this

Book1
ABCDEFGHIJ
1CityNameFirstSecondThirdOrdersOrder Numbers
2New YorkJames A11111115111701- James A1111
3New YorkJames B25502555256002- James A1115
4New YorkJames C12712812903- James A1117
5New YorkJames D04- James B2550
6New YorkJames E05- James B2555
7New YorkJames F06- James B2560
8New YorkJames G07- James C127
9New YorkJames H08- James C128
10New YorkJames I09- James C129
11
12
13
14
15
Sheet2
 
Assuming that the City and Name are in columns A and B of "Sheet1" and the Orders and Order Numbers are in columns A and B of "Sheet2", try this macro:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, sAddr As String, fnd As Range, name As Range, x As Long: x = 1
    Set desWS = Sheets("Sheet1")
    Set srcWS = Sheets("Sheet2")
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each name In desWS.Range("B2:B" & LastRow)
        Set fnd = srcWS.Range("A:A").Find(name, LookIn:=xlValues, lookat:=xlPart)
        If Not fnd Is Nothing Then
            sAddr = fnd.Address
            Do
                With desWS
                    .Cells(1, x + 2) = x
                    .Cells(name.Row, .Columns.Count).End(xlToLeft).Offset(0, 1) = fnd.Offset(, 1)
                    x = x + 1
                End With
                Set fnd = srcWS.Range("A:A").FindNext(fnd)
            Loop While fnd.Address <> sAddr
            sAddr = ""
        End If
        x = 1
    Next name
    Application.ScreenUpdating = True
End Sub
This is absolutely brilliant!!!. No idea how it is able to pull that data and transpose it, but it is AMAZING!!
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Assuming that the City and Name are in columns A and B of "Sheet1" and the Orders and Order Numbers are in columns A and B of "Sheet2", try this macro:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, sAddr As String, fnd As Range, name As Range, x As Long: x = 1
    Set desWS = Sheets("Sheet1")
    Set srcWS = Sheets("Sheet2")
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each name In desWS.Range("B2:B" & LastRow)
        Set fnd = srcWS.Range("A:A").Find(name, LookIn:=xlValues, lookat:=xlPart)
        If Not fnd Is Nothing Then
            sAddr = fnd.Address
            Do
                With desWS
                    .Cells(1, x + 2) = x
                    .Cells(name.Row, .Columns.Count).End(xlToLeft).Offset(0, 1) = fnd.Offset(, 1)
                    x = x + 1
                End With
                Set fnd = srcWS.Range("A:A").FindNext(fnd)
            Loop While fnd.Address <> sAddr
            sAddr = ""
        End If
        x = 1
    Next name
    Application.ScreenUpdating = True
End Sub
Hope you or @SQUIDD can help me troubleshoot this.

The codes do not work if the data becomes this.

Book1
ABC
1EntityName
2VerizonJohn Doe
3VerizonJane Doe
4VerizonJack Doe
5VerizonJose Doe
6VerizonJamie Doe
7VerizonJones Doe
8VerizonMike Doe
9VerizonMisty Doe
10Verizon
11Verizon
12Verizon
13Verizon
14Verizon
15
Sheet5


Book1
ABC
1NameOrder
2JOSE DOE123458
3JOSE DOE123473
4JOSE DOE123488
5JOSE DOE123503
6Mike Doe123518
7MIKE DOE123533
8MIKE DOE123548
9MIKE DOE123563
10Misty Doe123578
11Misty Doe123593
12Misty Doe123608
13Misty Doe123623
14John Doe123638
15John Doe123653
16John Doe123668
17John Doe123683
18John Doe123698
19James Doe123713
20James Doe123728
21James Doe123743
22James Doe123758
23James Doe123773
24James Doe123788
25Jane Doe123803
26Jane Doe123818
27Jane Doe123833
28Jane Doe123848
29Jack Doe123863
30Jack Doe123878
31Jack Doe123893
32Jack Doe123908
33
Sheet6
Cell Formulas
RangeFormula
B3:B32B3=B2+15


Is it because the data is unsorted?

Disregard the formula. I only put it there to fill the values.
 
Upvote 0
The sorting doesn't matter. This is what I get (changing the sheet names to Sheet5 and Sheet6):
EntityName12345
VerizonJohn Doe123638123653123668123683123698
VerizonJane Doe123803123818123833123848
VerizonJack Doe123863123878123893123908
VerizonJose Doe123458123473123488123503
VerizonJamie Doe
VerizonJones Doe
VerizonMike Doe123518123533123548123563
VerizonMisty Doe123578123593123608123623
Verizon
Verizon
Verizon
Verizon
Verizon


The result seems to be what you requested.
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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