简体   繁体   中英

Hi! How can I copy specific rows of data based on criteria and paste those to a new column on the same worksheet with a blank column between them

enter image description here Excel spreadsheet

Criteria for moving rows are based on column A = "Advia 1800 2" and column B = "15941".

If criteria are met, then copy and paste these rows to column I on the same worksheet under the headings as in the attached picture.

I really appreciate if someone can show me what the macro will look like.

  1. You also have an additional friend and that one is Macro Recorder. You can start recording your actions by selecting Record Macro It can be selected from Views->Macros-> Record Macro in Views Tab or in Developer Tab in code section or by clicking its symbol near bottom left corner side of your worksheet.

  2. Now after clicking record macro, a popup appears and you can okay it after either noting macro name or change it to new one. Let us ignore shortcut key at this stage.

  3. Now Macro has started recording your actions do manually what you want to do. Select Range to be filtered, Select Filter in Data Tab. Apply criteria from drop downs in Column 1 and then in Column 2. Go to Special in Find and Select section drop down and select visible cells and then copy. Select I1 cell and Paste. Go to Data Tab and click Filter again. Now you will see results in the sheet as per your requirements. Code generated by Macro recorder is:

      Sub Macro5() ' ' Macro5 Macro ' ' Range("A1:G14").Select Selection.AutoFilter ActiveSheet.Range("$A$1:$G$14").AutoFilter Field:=1, Criteria1:= _ "Advia 1800 2" ActiveSheet.Range("$A$1:$G$14").AutoFilter Field:=2, Criteria1:="15941" Selection.SpecialCells(xlCellTypeVisible).Select Selection.Copy Range("I1").Select ActiveSheet.Paste Application.CutCopyMode = False Selection.AutoFilter End Sub 

Results are shown in the snapshot.

结果

Still no reason to cheer as it has select statements. Please go through How to avoid using Select in Excel VBA macros as an exercise for you and modify the code to remove select in macro.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM