Home

Macro to find Data, copy the entire row?

%3CLINGO-SUB%20id%3D%22lingo-sub-376287%22%20slang%3D%22en-US%22%3EMacro%20to%20find%20Data%2C%20copy%20the%20entire%20row%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-376287%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20large%20spreadsheet%20sent%20to%20me%20twice%20a%20month.%26nbsp%3BI%20can%20record%20a%20couple%20of%20macros%20that%20help%20me%20delete%20the%20columns%20I%20don't%20need%20and%20to%20sort%20the%20data%2C%20but%20I%20don't%20know%20how%20to%20proceed%20from%20there.%20I%20need%20to%20find%20a%20value%20in%20Column%20F%2C%20select%20all%20Rows%20that%20contain%20that%20data%2C%20and%20then%20copy%20all%20of%20the%20Rows%20to%20a%20new%20sheet.%20If%20I%20try%20to%20record%20a%20macro%20while%20doing%20that%20via%20CTRL%20F%2C%20CTRL%20A%2C%20Close%2C%20CTRL-%2C%20etc.%20the%20resulting%20macro%20only%20selects%20the%20%22Row%20Numbers%22%20that%20contain%20the%20data%2C%20and%20then%20goes%20on%20from%20there.%20Since%20the%20actual%20number%20of%20the%20rows%20that%20contain%20the%20data%26nbsp%3Bwill%20change%20twice%20a%20month%20it%20won't%20work%20for%20me.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20what%20it%20generates%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CEM%3ESub%20Macro910()%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3E'%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3E'%20Macro910%20Macro%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3E'%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CEM%3E'%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Rows(%22981%3A3588%22).Select%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Range(%22F981%22).Activate%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Selection.Copy%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Sheets.Add%20After%3A%3DActiveSheet%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ActiveSheet.Paste%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Range(%22A1%22).Select%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3EEnd%20Sub%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20to%20get%20a%20macro%20to%20find%20the%20data%20in%20Column%20F%2C%20select%20all%20of%20the%20rows%20that%20contain%20the%20data%2C%20and%20copy%20all%20of%20the%20rows%20to%20a%20new%20sheet%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20greatly%20appreciated.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-376287%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-377531%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%20to%20find%20Data%2C%20copy%20the%20entire%20row%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-377531%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F302749%22%20target%3D%22_blank%22%3E%40GiraffeCat%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CSPAN%3ESlightly%20cleaner%20code%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3ESub%20ExtractData()%3CBR%20%2F%3EDim%20rng%20As%20Range%3CBR%20%2F%3E'Clear%20the%20content%20of%20the%20destination%20Table%3CBR%20%2F%3ESet%20rng%20%3D%20ActiveSheet.ListObjects(%22DataExtract%22).DataBodyRange%3CBR%20%2F%3Erng.ClearContents%3CBR%20%2F%3E'Filter%20the%20source%20Table%3CBR%20%2F%3EActiveSheet.ListObjects(%22DataTable%22).Range.AutoFilter%20Field%3A%3D1%2C%20Criteria1%3A%3D%20_%3CBR%20%2F%3ERange(%22Criterion%22).Value%3CBR%20%2F%3E'Copy%20filtered%20data%20from%20source%20to%20destination%3CBR%20%2F%3ERange(%22DataTable%22).Copy%20_%3CBR%20%2F%3EDestination%3A%3Drng.Item(1)%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-377503%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%20to%20find%20Data%2C%20copy%20the%20entire%20row%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-377503%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F302749%22%20target%3D%22_blank%22%3E%40GiraffeCat%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%22%3CEM%3EI%20was%20just%20hoping%20for%20a%20more%20automated%20way%20to%20do%20it%3C%2FEM%3E%22%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EYou%20are%20almost%20there%2C%20then.%26nbsp%3B%20All%20you%20need%20to%20find%20now%20is%20how%20to%20change%20the%20data%20source%20(a%20different%20file%20to%20replace%20the%20first%20or%20different%20data%20inserted%20into%20a%20given%20table)%20and%20hit%20'refresh'.%26nbsp%3B%20All%2010%20cities%20at%20one%20click!%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EOn%20the%20VBA%20front%2C%20I%20would%20never%20use%20a%20direct%20cell%20reference%20from%20VBA.%26nbsp%3B%20Even%20using%20'macro%20recorder'%20style%20of%20coding%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%22Sub%20ExtractData()%3CBR%20%2F%3EActiveSheet.ListObjects(%22DataTable%22).Range.AutoFilter%20Field%3A%3D1%2C%20Criteria1%3A%3D%20%22MA%22%3CBR%20%2F%3ERange(%22DataTable%22).Select%3CBR%20%2F%3ESelection.Copy%3CBR%20%2F%3ERange(%22Extract%22).Select%3CBR%20%2F%3EActiveSheet.Paste%3CBR%20%2F%3EEnd%20Sub%22%3C%2FSPAN%3E%3C%2FP%3E%3CP%3Em%3CSPAN%3Eeans%20that%20you%20do%20not%20have%20to%20change%20the%20code%20each%20time%20the%20table%20%22DataTable%22%20resizes%20or%20even%20moves.%26nbsp%3B%20I%20would%20also%20prefer%20to%20eliminate%20'select'%20from%20my%20code%20but%20that%20would%20require%20me%20to%20learn%20more%20about%20ways%20of%20transferring%20filtered%20data%20to%20code%20rather%20than%20the%20underlying%20dataset.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-377312%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%20to%20find%20Data%2C%20copy%20the%20entire%20row%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-377312%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20been%20using%20Power%20Query%20%22From%20Table%22%20since%20I%20volunteered%20to%20help%20out.%20I%20first%20sort%20it%20out%20by%26nbsp%3Bstate%2C%20then%20by%20the%20desired%20city%2C%20and%20then%20%22Close%20and%20Load%22%20to%20a%20new%20sheet.%20I%20do%20this%20for%20the%26nbsp%3B%2010%20specific%20cities%20need%20to%20track.%26nbsp%3B%26nbsp%3BI%20was%20just%20hoping%20for%20a%20more%20automated%20way%20to%20do%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20no%20idea%20how%20to%20use%20%22Tables%20for%20data%20transfer%2C%20so%20it%20sounds%20like%20I%20may%20have%20to%20take%20a%20class%2C%20or%20ten%2C%20to%20learn%20VBA.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20help%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-376476%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%20to%20find%20Data%2C%20copy%20the%20entire%20row%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-376476%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F302749%22%20target%3D%22_blank%22%3E%40GiraffeCat%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20recommend%20using%20Tables%20for%20data%20transfer%20between%20the%20worksheet%20and%20VBA%20macros.%26nbsp%3B%20Table%20references%20adjust%20to%20match%20the%20actual%20data.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERather%20than%20learning%20VBA%2C%20however%2C%20you%20might%20find%20it%20easier%20and%20more%20effective%20to%20use%20the%20Power%20Query%20tools%20(Get%20and%20Transform)%20on%20the%20'Data'%20Ribbon%20Tab.%26nbsp%3B%20That%20will%20take%20data%20into%20memory%20from%20a%20Table%20(or%20even%20from%20other%20files%20or%20databases)%20and%20it%20provides%20all%20the%20options%20you%20are%20likely%20to%20need%20for%20filtering%2C%20sorting%2C%20appending%20data%20sets%2C%20merging%20%3CEM%3Eetc%3C%2FEM%3E.%20before%20loading%20the%20result%20back%20to%20a%20fresh%20Table.%26nbsp%3B%20It%20will%20all%20be%20very%20unfamiliar%20at%20first%20but%20the%20functionality%20is%20purpose-written%20for%20the%20type%20of%20work%20you%20have%20described.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
GiraffeCat
New Contributor

I have a large spreadsheet sent to me twice a month. I can record a couple of macros that help me delete the columns I don't need and to sort the data, but I don't know how to proceed from there. I need to find a value in Column F, select all Rows that contain that data, and then copy all of the Rows to a new sheet. If I try to record a macro while doing that via CTRL F, CTRL A, Close, CTRL-, etc. the resulting macro only selects the "Row Numbers" that contain the data, and then goes on from there. Since the actual number of the rows that contain the data will change twice a month it won't work for me.

 

Below is what it generates:

 

Sub Macro910()
'
' Macro910 Macro
'

'
    Rows("981:3588").Select
    Range("F981").Activate
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Range("A1").Select
End Sub

 

Is there a way to get a macro to find the data in Column F, select all of the rows that contain the data, and copy all of the rows to a new sheet?

 

Any help would be greatly appreciated.

5 Replies

@GiraffeCat 

I would recommend using Tables for data transfer between the worksheet and VBA macros.  Table references adjust to match the actual data.

 

Rather than learning VBA, however, you might find it easier and more effective to use the Power Query tools (Get and Transform) on the 'Data' Ribbon Tab.  That will take data into memory from a Table (or even from other files or databases) and it provides all the options you are likely to need for filtering, sorting, appending data sets, merging etc. before loading the result back to a fresh Table.  It will all be very unfamiliar at first but the functionality is purpose-written for the type of work you have described.

@Peter Bartholomew 

 

I have been using Power Query "From Table" since I volunteered to help out. I first sort it out by state, then by the desired city, and then "Close and Load" to a new sheet. I do this for the  10 specific cities need to track.  I was just hoping for a more automated way to do it.

 

I have no idea how to use "Tables for data transfer, so it sounds like I may have to take a class, or ten, to learn VBA.

 

Thank you for your help

@GiraffeCat 

"I was just hoping for a more automated way to do it"

You are almost there, then.  All you need to find now is how to change the data source (a different file to replace the first or different data inserted into a given table) and hit 'refresh'.  All 10 cities at one click!

 

On the VBA front, I would never use a direct cell reference from VBA.  Even using 'macro recorder' style of coding:

"Sub ExtractData()
ActiveSheet.ListObjects("DataTable").Range.AutoFilter Field:=1, Criteria1:= "MA"
Range("DataTable").Select
Selection.Copy
Range("Extract").Select
ActiveSheet.Paste
End Sub"

means that you do not have to change the code each time the table "DataTable" resizes or even moves.  I would also prefer to eliminate 'select' from my code but that would require me to learn more about ways of transferring filtered data to code rather than the underlying dataset.

@GiraffeCat 

Slightly cleaner code

Sub ExtractData()
Dim rng As Range
'Clear the content of the destination Table
Set rng = ActiveSheet.ListObjects("DataExtract").DataBodyRange
rng.ClearContents
'Filter the source Table
ActiveSheet.ListObjects("DataTable").Range.AutoFilter Field:=1, Criteria1:= _
Range("Criterion").Value
'Copy filtered data from source to destination
Range("DataTable").Copy _
Destination:=rng.Item(1)
End Sub

Peter,

So far I have it working with five "recorded" macros - two of which use power query. It is down to about 18 mouse clicks, so I am happy. I will continue to learn how to do this stuff....

Thank you so much for taking the time to help me out.

Dan
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies