Excel VBA: Filter and copy from top 5 rows/cells -


i have data table sorted on descending order in column f. need copy top 5 rows, data column a, b, d, , f (not headers). see pictures.

sub top5()  sheets("sheet1").select  if (activesheet.autofiltermode , activesheet.filtermode) or activesheet.filtermode activesheet.showalldata end if   activesheet.range("$a$4:$t$321").autofilter field:=3, criteria1:="dave" activeworkbook.worksheets("sheet1").autofilter.sort.sortfields. _     clear activeworkbook.worksheets("sheet1").autofilter.sort.sortfields.add _     key:=range("f4:f321"), sorton:=xlsortonvalues, order:=xldescending, _     dataoption:=xlsorttextasnumbers activeworkbook.worksheets("sheet1").autofilter.sort     .header = xlyes     .matchcase = false     .orientation = xltoptobottom     .sortmethod = xlpinyin     .apply end  ' copy-paste part supposed to, specific  ' cells.  not generalised , have repeat operation ' several times different people sheets("sheet1").select range("a3:b15").select selection.copy  sheets("sheet2").select range("a3").select activesheet.paste  sheets("sheet1").select range("d3:d15").select application.cutcopymode = false selection.copy  sheets("sheet2").select range("c3").select activesheet.paste  sheets("sheet1").select range("f3:f15").select application.cutcopymode = false selection.copy  sheets("sheet2").select range("d3").select activesheet.paste application.cutcopymode = false  end sub 

i thought trying adapt snippet of code below using visible cells function, i'm stuck , can't find on net fits.

' selects rows (plus 1, due offset), want parts of top 5. sheets("sheet1").select activesheet.range("$a$4:$b$321").offset(1, 0).specialcells(xlcelltypevisible).select selection.copy sheets("sheet2").select range("a3").select activesheet.paste  sheets("sheet1").select activesheet.range("$d$4:$d$321").offset(1, 0).specialcells(xlcelltypevisible).select selection.copy sheets("sheet2").select range("c3").select activesheet.paste 

i hope example makes sense , appreciate help!

sample excel table

note: heading names same in 2 tables show data same. headers not supposed copied. in addition, there column/white space in second table. solution should include this.

data copied new table

firstly few helpful points:

  • you should refer worksheets there code name avoid renaming issues.
  • if want work vba advice avoid merged cells plague. cause havoc code. if possible use format cells - alignment - horizontal - centre accross selection
  • i advise avoiding loops wherever possible , take advantage of excels built in functions instead practice exercise.

here solution. keep simple. if need further let me now.

sub hth()      dim rcopy range      sheet1.autofilter.range         '// set somewhere blank , unused on worksheet         set rcopy = sheet1.range("a" & rows.count - (.rows.count))         .specialcells(xlcelltypevisible).copy rcopy     end      rcopy.offset(1).resize(5) '// offset avoid header         .resize(, 2).copy sheet2.range("a5")         .offset(, 3).resize(, 1).copy sheet2.range("d5")         .offset(, 5).resize(, 1).copy sheet2.range("f5")         .currentregion.delete xlup '// delete tempory area     end      set rcopy = nothing  end sub 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -