Macro to Search a List then write selective items to another sheet

rwmill9716

Active Member
Joined
May 20, 2006
Messages
493
Office Version
  1. 2013
Platform
  1. Windows
The sheet shown, Complaints, lists on each row information on a single complaint. This information extends from Col A to Col Y (though not that way here, the data actually begins in row 3). I want to highlight a Customer cell, say H227 for Customer A, then activate a macro that (1) recognizes the active customer (here A), searches the list, identifies each row where A is the customer and then writes each row into sheet "Event" beginning in row 4. I should then see the same A to Y cells of information, beginning in row 4, filled in sheet Event for the six customer A complaints. Note, the actual data in sheet Complaints involves 300 rows of data, and over 100 customers can be on the list, some up to 15 times. I know that I could do this by filtering the data, copying and pasting, but a simple macro would be quicker for the group of individuals involved. Note, I then use this information to populate a chart.

Thanks very much for your help,

ric




Excel 2010
ABCDEFGHIJKLMNOPQRSTUVWXY
226#INITIATION MonthINITIATION DATECRM Transaction IDSales OrderSalesSHIP PlantCUSTOMERCITYST/PROVPRODUCTProductLbs InvolvedPROBLEMComplaint CategoryComplaint Sub CategoryINITIATORINVESTIGATORSTATUSCARRIERDATE CLOSEDDAYS TO CLOSEMonth ClosedMonth ClosedDays Outstanding
2271Nov11/09/125114885301253256CCBASjarkajDubaiNTA,POWDER,BAG25KG,40BAGS/PNTA44,000Goods Infested with TermitesPack-OutOtherMsenteLarry LavoieComplete1/8/2013601Jan1
2282Dec12/05/125115193301260656ACBBGuangzhouCHINANTA,POWDER,BAG,25KG,40BAGS/P,ASIANTA2,200Quality IssuesQualityProduct PerformanceSSSUJ. PerkinsComplete2/5/2013622Feb2
2293Dec12/11/12511532530122111BDecBShanghaiChinaINT,HMD,REFINED,100%,BULK,SOLIDHMD3,000Weight DiscrepencyPack-OutWeight DiscrepancyDAMARRRussell HusfeldComplete1/22/2013421Jan3
2304Dec12/12/125115332301264681ACBCQBCanadaNTA,POWDER,UNLINEDNTA11,0135 SS Not ShippedDistrib/WarehousePicking & PackingMBSCHEChristine IaconisComplete1/22/2013411Jan4
2315Dec12/12/125115343301261675ADecDShanghaiChinaINT,HMD,REFINED,100%,Bulk SolidHMD7,339Low ISO WeightsPack-OutWeight DiscrepancyDAMARRRussell HusfeldComplete1/22/2013411Jan5
2326Jan01/07/135115715301260865ADecAGermanyINT,HMD,Refined,100%,Bulk, SolidHMD40,393Port date should be delivery dateTransportationDelivery - LateD.MarronD. MarronComplete1/16/201391Jan6
2337Jan01/08/135115760301266808APnsACincinnatiOHAdipic, Resin, 258, FF, 2000lb, C-Bag, 45x45PADIPIC44,000Carrier ran out of hoursTransportationDelivery - LateW.GummereTim MartinCompleteCH Robinson1/16/201381Jan7
2348Jan01/09/135115783301266743APnsAKingsportTnAdipic, Resin, 220, FF, 2205lb, 1Bag/PADIPIC88,200Late deliveries (2)TransportationDelivery - LateW.GummereTim MartinCompleteHeartland Express1/29/2013201Jan8
Complaints
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try:
Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim bottomH As Long
    bottomH = Range("H" & Rows.Count).End(xlUp).Row
    Dim customer As String
    Dim rng As Range
    Dim x As Long
    x = 4
    customer = InputBox("Please enter customer.")
    For Each rng In Range("H3:H" & bottomH)
        If rng = customer Then
            rng.EntireRow.Copy Sheets("Events").Cells(x, 1)
            x = x + 1
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you very much, Mumps. This works as desired, but I need one modification if possible.

What's happening now is when I activate the macro, a window pops up wanting me to type the customer's name, then it executes as desired. The problem is that some of these names are very involved (not the simple A, B C that I used in the example) so I need an easier way to enter the name. Could instead I copy the customer's name to cell A1 (in sheet Complaints), and the macro read that as the customer's name. Or even better, could I activate a particular cell with the customer's name (make that cell the active cell by clicking the mouse on that cell) in sheet Complaints and the macro take that as the customer that I'm interested in? In the latter case, the name could be taken anywhere in Column H.
 
Upvote 0
Try this macro. First select the cell in column H and then run the macro.
Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim bottomH As Long
    bottomH = Range("H" & Rows.Count).End(xlUp).Row
    Dim rng As Range
    Dim x As Long
    x = 4
    For Each rng In Range("H3:H" & bottomH)
        If rng = ActiveCell.Value Then
            rng.EntireRow.Copy Sheets("Events").Cells(x, 1)
            x = x + 1
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Mumps last macro above works well: Once a customer's name is highlighted, it scans the data set in tab "Complaints", and copies an entire row into tab "Events" for every row in "Complaints" that that customer's name appeared. For example, "Complaints" may have 200 entries with 15 associated with customer A. "Events" will then have 15 rows.

I need an adjustment for this macro that only copies and pastes A to Y columns and nothing beyond that as I want to do some computations with the customer's data in "Events."

Thanks again,

Ric
 
Upvote 0
Hi Ric. Change this line:
Code:
 rng.EntireRow.Copy Sheets("Events").Cells(x, 1)
to this line:
Code:
 Range("A" & rng.Row & ":Y" & rng.Row).Copy Sheets("Events").Cells(x, 1)
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,346
Members
449,155
Latest member
ravioli44

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