excel - copying data from another workbook -
i trying replace current data in file data in selected file have same attributes. want replace data a1:q in current file other selected file. tried writing code showing errors .
sub newdata() set appxl = createobject("excel.application") dim myfile window dim currentsheet worksheet dim lastrow double dim sourcefilename string 'open source file.xlsx appxl vfile = application.getopenfilename(title:="select file opened") if vfile = false exit sub 'if user didn't select file, exit sub ' set myfile = workbooks.open(vfile) .visible = false end 'get first sheet data set myfile = appxl.windows(vfile) myfile.activate set currentsheet = myfile.sheets(1) 'past table in current excel file lastrow = currentsheet.range("a1").end(xldown).row sheets("data retrieval").range("a1:e" & lastrow) = currentsheet.range("a1:q" & lastrow).value 'close source file.xlsx appxl.workbooks(vfile).close end sub
this not prettiest of codes works asked!
sub newdata() dim myfile window dim currentsheet worksheet dim lastrow double dim sourcefilename string dim sourcefileworksheet string dim destinationwb string dim destinationwksheet string 'set destination worksheet open workbook when run macro destinationwb = activeworkbook.name destinationwksheet = activesheet.name 'select source file dim fnameandpath variant, wb workbook fnameandpath = application.getopenfilename(title:="select file opened") ' cancel macro if nothing selected if fnameandpath = false msgbox ("nothing selected, macro cancelled") exit sub end if 'open source file.xlsx workbooks.open (fnameandpath) 'set source names sourcefilename = activeworkbook.name sourcefileworksheet = activesheet.name 'determine last row of source lastrow = workbooks(sourcefilename).worksheets(sourcefileworksheet).range("a1").end(xldown).row 'past table in current excel file - note should change range of destination a1:q if want copied workbooks(destinationwb).worksheets(destinationwksheet).range("a1:e" & lastrow) = workbooks(sourcefilename).worksheets(sourcefileworksheet).range("a1:q" & lastrow).value 'close source file.xlsx workbooks(sourcefilename).close 'confirm complete msgbox ("complete!") end sub
Comments
Post a Comment