excel - Copying and pasting data using VBA code -
i have button on spreadsheet that, when pressed, should allow user open file, copy columns a-g of spreadsheet "data", paste data columns on current sheet.
i have logic error in code; runs, pastes selection in wrong place.
i having trouble referencing 2 workbooks.
here code:
sub button1_click() dim excel excel.application dim wb excel.workbook dim sht excel.worksheet dim f object set f = application.filedialog(3) f.allowmultiselect = false f.show set excel = createobject("excel.application") set wb = excel.workbooks.open(f.selecteditems(1)) set sht = wb.worksheets("data") sht.activate sht.columns("a:g").select selection.copy range("a1").select activesheet.paste wb.close end sub
use pastespecial method:
sht.columns("a:g").copy range("a1").pastespecial paste:=xlpastevalues
but big problem you're changing activesheet "data" , not changing back. don't need activate , select, per code (this assumes button on sheet want copy to).
Comments
Post a Comment