vba - Excel set active Workbook -


i searched forum , couldn't find answer fit problem. pretty new excel vba , having trouble activating workbook opened. directly below part causing me trouble.

so press button , brings me file path, , select file open. every week file has different name , not sure until open file path , @ it.

once open it, macro manipulate data in file , copy , paste workbook running code. when run macro , open file not activate newly opened workbook , runs rest of macro trying manipulate data in original file.

i think either need open file differently workbook opened active 1 or figure out how activate newly opened workbook without knowing file name. thank help.

dim filepath string filepath = environ("userprofile") & "\dropbox\on go ordering" call shell("explorer.exe" & " " & filepath, vbnormalfocus)   range("a6:e500").select 

sub on_the_go_button()      dim ranker workbook      set ranker = thisworkbook      dim filepath string     filepath = environ("userprofile") & "\dropbox\on go ordering"     call shell("explorer.exe" & " " & filepath, vbnormalfocus)       range("a6:e500").select     selection.copy     sheets.add after:=sheets(sheets.count)     activesheet.paste     application.cutcopymode = false     selection.autofilter     activesheet.range("$a$1:$e$495").autofilter field:=1, criteria1:=rgb(213, _         223, 248), operator:=xlfiltercellcolor     range("g1").select     activecell.formular1c1 = _         "=if(right(rc[-6],8)=""subtotal"",value(left(rc[-6],6)),"""")"     range("h1").select     activecell.formular1c1 = "=if(rc[-1]="""","""",rc[-4])"     range("i1").select     activecell.formular1c1 = "=if(rc[-1]="""","""",rc[-4])"     range("g1:i1").select         selection.copy     range("g1:i500").select     selection.pastespecial paste:=xlpasteformulas, operation:=xlnone, _     skipblanks:=false, transpose:=false     selection.copy     ranker.activate     range("a1").select     selection.pastespecial paste:=xlpastevalues, operation:=xlnone, skipblanks _         :=false, transpose:=false      range("p1:q74").select     selection.copy     sheets("contest").select     range("a3").select      activecell.end(xltoright).select       activecell.offset(0, 1).select      selection.pastespecial paste:=xlpastevalues, operation:=xlnone, skipblanks _         :=false, transpose:=false end sub 

because op specified default path; recommend using application.filedialog(msofiledialogfilepicker)

enter image description here

usage:

dim weeklyworkbook  set weeklyworkbook = getweeklyworkbook  if weeklyworkbook nothing     msgbox "no file selected", vbinformation, "action cancelled"     exit sub end if 

function getweeklyworkbook() workbook      dim fdialog filedialog, result integer     set fdialog = application.filedialog(msofiledialogfilepicker)      'optional: filedialog properties     fdialog.allowmultiselect = false     fdialog.title = "select file"     fdialog.initialfilename = environ("userprofile") & "\dropbox\on go ordering"     'optional: add filters     fdialog.filters.clear     fdialog.filters.add "excel files", "*.xls, *.xlsx, *.xlsm"     fdialog.filters.add "all files", "*.*"      'show dialog. -1 means success!     if fdialog.show = -1         set getweeklyworkbook = workbooks.open(fdialog.selecteditems(1))     end if  end function 

reference: vba filedialog – opening, selecting , saving files , folders


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -