VBA - excel closes the previous workbook on opening the new one -


i have strange problem, suscpect it's connected version of excel, i'm not sure @ all. can't figure out alone need help. have macro, operates on fresh workbook - it's not saved anywhere, worker save manually afterwards. macro .xlam format add-in, adding couple of buttons ribbon , these buttons start code. inside code have simple lines opening new workbook, chosen earlier user:

application.displayalerts = false     set wbmpa = workbooks.open(mpa_file) application.displayalerts = true 

earlier, code sets active workbook object/workbook macro work on (tried both versions):

set dwb = application.activeworkbook 

and later in code

dwb.activate 

or:

dwb = activeworkbook.name 

and then

workbooks(dwb).activate 

the lines in separate subs, variable globally declared.

the code works fine until opening of wbmpa (watching in locals time). when try open new file code above, earlier workbook (dwb) closes unknown reasons. error 1st method this: error screenshot second 1 gives simple "subscipt out of range". errors, however, not problem. problem cause of them, closing of workbook unknown reasons.

it happens when open new workbook (using excel icon on start bar) - when file -> new -> blank workbook using opened workbook, error not occur.

another strange thing - me , colleague work use 2013 version of excel. never have error, has every time.

this general scheme of code, other things meaningless in case because there no other manipulations of workbooks/worksheets.

dim dwb object dim wbmpa object  sub_1()  set dwb = activeworkbook set wbmpa = workbooks.open(mpa_file)  call sub_2  end sub    sub_2()  dwb.activate  end sub 

i error on activation of dwb in sub_2, because closes god knows reason on opening of wbmpa in sub_1.

if have only opened blank workbook (clicking excel toolbar, example) , open named workbook before making any changes blank workbook, blank workbook disappear. believe normal/expected behavior.

i can't speculate why happens on 1 computer not another, how have observed new/blank documents (excel, powerpoint, word) behave, , assume normal behavior. may have different option/configuration on excel environment changing default behavior, or maybe altering blank file before running macro, , co-worker isn't, etc.

a word of caution avoid relying on activeworkbook -- , in case if expectation set dwb new/blank workbook, best way explicitly create new/blank workbook, rather relying on user manually open new/blank target workbook.

set dwb = workbooks.add 

if, on other hand dwb must assigned other known/existing workbook, should either providing file path open statement, or workbook name workbooks collection.

on related note, it's never necessary activate workbook, see here:

how avoid using select in excel vba macros

and further note: variables aren't globally scoped, they're scoped module using dim statement. public declaration uses public keyword, not dim keyword. both module-scoped , global-scoped should used caution (public moreso module-scoped) , in cases it's preferable pass objects/variables reference dependent subs , functions:

how make excel vba variables available multiple macros?


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -