excel vba - VBA Open any workbook -
i have macro opens spreadsheet specific folder , saves output worksheet called sheet1 in workbook. macro works if file name called "myfile.xls" able run on file name must have "book2" worksheet.
here code:
dim source workbook dim output workbook dim sourcesheet worksheet dim outputsheet worksheet dim file string file = "c:\spreadsheets\myfile.xls" 'i handle files location' set output = thisworkbook output.activate if len(dir$(file)) > 0 set source = workbooks.open(file) set sourcesheet = source.worksheets("book2") 'must run if sheet called book2' set outputsheet = output.worksheets("sheet1") 'saves sheets new sheet called sheet1' end sub
is trying? (tried , tested)
sub sample() dim source workbook, output workbook dim sourcesheet worksheet, outputsheet worksheet dim file '~~> show dialog open excel file file = application.getopenfilename("excel files (*.xls*), *.xls*") if file = false exit sub set output = thisworkbook if len(dir$(file)) > 0 set source = workbooks.open(file) '~~> error check see if workbook has sheet on error resume next set sourcesheet = source.worksheets("book2") if err.number = 0 set outputsheet = output.worksheets("sheet1") ' '~~> rest of code ' else msgbox "not found" source.close savechanges:=false end if on error goto 0 end if end sub
Comments
Post a Comment