copy - Excel VBA: Copying the desired data -
i need write code copy data pertaining each unique id new csv file (meaning 1 csv file each unique id). i'm new excel vba. able copy entire data csv file. however, fail each unique id.
sub exportcontract(control iribboncontrol) if thisworkbook.activesheet.name <> "contract" msgbox "please select contract tab , run again!" exit sub end if application.screenupdating = false accountname = thisworkbook.sheets("information").range("b4") path = application.activeworkbook.path sheets("contract").select sheets("contract").copy activeworkbook.saveas filename:=path & "\" & accountname & "_contract.csv", fileformat:=xlcsv, createbackup:=false activewindow.close end sub
now, not code snip. but, real program incorporating solution provide, into.
you need start @ first cell, move down , export each row. you're exporting csv, easier export writing text file, not via "saveas"
here code started:
public sub export() '//sort data first activeworkbook.worksheets("sheet1").sort .setrange range("a2:b11") .header = xlno .matchcase = false .orientation = xltoptobottom .sortmethod = xlpinyin .apply end '//select first cell thisworkbook.sheets(1).range("a2").select dim sfilename string dim id long while activecell.value <> "" '//loop until have gone on cells '//get value , file name id = activecell.value sfilename = thisworkbook.path & "\" & id & ".csv" open sfilename output #1 '//keep going until have dealt id while activecell.value = id print #1, activecell.value & "," & activecell.offset(0, 1).value activecell.offset(1, 0).select wend '//close file close #1 wend end sub
Comments
Post a Comment