-1

I have a strange problem, I suscpect it's connected to the version of the Excel, but I'm not sure at all. I can't figure it out alone so I need your help. I have a macro, which operates on a fresh workbook - it's not saved anywhere, as the worker will save it manually afterwards. The macro is a .xlam format add-in, adding a couple of buttons to the ribbon and these buttons start the code. Inside the code I have simple lines for opening a new workbook, chosen earlier by an user:

Application.DisplayAlerts = False
    Set wbMPA = Workbooks.Open(MPA_file)
Application.DisplayAlerts = True

Earlier, the code sets active workbook as an object/workbook the macro will mainly work on (tried both versions):

Set dwb = Application.ActiveWorkbook

and later in the code

dwb.activate

OR:

dwb = ActiveWorkbook.Name

and then

workbooks(dwb).Activate

The lines are in separate subs, but the variable is globally declared.

The code works fine until the opening of wbMPA (watching it in the locals all the time). When I try to open the new file with the code above, the earlier workbook (dwb) just closes itself from unknown reasons. The error I get from the 1st method is this: error screenshot The second one gives a simple "Subscipt out of range". The errors, however, are not a problem. The problem is the cause of them, which is closing of the workbook from unknown reasons.

It happens only when I open the completely new workbook (using the excel icon on the Start bar) - when I do it from File -> New -> Blank Workbook using already opened workbook, the error does not occur.

Another strange thing - me and my colleague from work use 2013 version of Excel. I never have this error, she has it every time.

This is a general scheme of the code, other things are meaningless in this case because there are no other manipulations of workbooks/worksheets.

Dim dwb As Object
Dim wbMPA As Object

Sub_1()

Set dwb = ActiveWorkbook
Set wbMPA = Workbooks.Open(MPA_file)

Call Sub_2

End Sub 


Sub_2()

dwb.Activate

End Sub

I get an error on the activation of dwb in Sub_2, because it closes itself for God knows what the reason on the opening of wbMPA in the Sub_1.

kshaq
  • 25
  • 6
  • *Earlier, the code sets active workbook as an object/workbook the macro will mainly work on* **<** This seems relevant, why don't you include it? – David Zemens Apr 06 '17 at 14:52
  • *However, it causes an error before it starts for good,* **<** This also seems relevant, you should include what error message and what line raises the error. – David Zemens Apr 06 '17 at 14:52
  • It's a generic set line: set asd = ActiveWorkbook. This one gives me the error about workbook not existing (it's closed, so kinda logical). I also tried declaring the name of the workbook as a variable, and then activating it with workbooks(asd).activate. This one gives me subscript out of range (it's closed, so obvious too.) – kshaq Apr 06 '17 at 14:57
  • 3
    No, none of what you describe is obvious. Please revise your question to include additional code (it is difficult to read bits of code in the comments) and identify which specific error raises at which line. Right now, your comment is ambiguous ("this one gives me the error about workbook not existing") -- *which* workbook doesn't exist? If it doesn't exist why are you trying to `Set` it or `Open` it ? – David Zemens Apr 06 '17 at 14:59
  • I try to set it, because it should be already opened before the macro starts, but it's being closed at the opening of another workbook(code lines above). I open the blank workbook, not saved anywhere, and then start the macro. I have no lines closing the workbook included anywhere in the code. – kshaq Apr 06 '17 at 15:04
  • I would give you the screenshots of errors, but at the moment I have no access to her computer, and on mine the problem is not existing. – kshaq Apr 06 '17 at 15:07
  • What part of "please revise your question..." did you not understand? I am not asking you to elaborate in the comments, I'm asking you please to ask a good question, and to provide (in the question body, **not** in disjointed comments) sufficient information/detail to attempt to diagnose the problem and the flow of your program. Until and unless you do that, literally nobody will be able to help you. – David Zemens Apr 06 '17 at 15:08
  • It might actually help to show your full code, because "earlier" in the code simply doesn't give enough context about what is happening or what might be happening in the intervening lines, or in other procedures. Are there event procedures you're not considering which might also be a factor here? – David Zemens Apr 06 '17 at 15:29
  • 1
    If you're trying to `Set` a workbook that's closed, you need to either open it, or use the full path. Also, your line `dwb = ActiveWorkbook.Name` should only work if `dwb` is a `String`. Please also see [How to Ask](https://stackoverflow.com/help/how-to-ask). We don't need a screenshot of the error necessarily, just the type of error, and the code the error is triggering on. ..plus any relevant code too. Otherwise, this is pretty broad. How did you declare your variables? – BruceWayne Apr 06 '17 at 15:37
  • Guys, I know this all, I know what these errors mean, we're missing the point here. I'm trying to figure out, why this workbook closes, when I didn't tell him to, and it's not even occurring in every scenario of macro startup. I cannot put the whole code here as I'm restricted by the company rules, but I'll prepare a scheme of it and put here later. – kshaq Apr 06 '17 at 15:58
  • Have you made any attempts to debug this manually? If so, revise further your question and include those details. If not, I would suggest doing so. Put a breaktpoint on the `Set wbMPA = Workbooks.Open(MPA_file)` line, then step through the code using F8 in debug mode and observe what is happening... – David Zemens Apr 06 '17 at 16:35
  • *It happens only when I open the completely new workbook (using the excel icon on the Start bar)* OK. Now, where does this macro exist? It certainly doesn't exist in the completely new workbook. Is it an Add-in? Otherwise, you're still not fully describing your environment. – David Zemens Apr 06 '17 at 16:37
  • 1
    Yes, it's an add-in, .xlam, adding a couple of new buttons to the ribbon and these buttons start the macro. I tried to debug it whole day today, and it just closes after the opening of a new workbook, so the variables are empty and this leads to errors. And still - on my PC the error does not exist. I guess this could be the trace. – kshaq Apr 06 '17 at 16:51

1 Answers1

2

If you have only opened a blank workbook (clicking Excel from Toolbar, for example) and then you open any named workbook before making any changes to the blank workbook, the blank workbook will disappear. I believe that is normal/expected behavior.

I can't speculate why this happens on one computer but not another, but this is always how I have observed new/blank documents (Excel, PowerPoint, Word) to behave, and assume this to be the normal behavior. You may have some different option/configuration on your Excel environment which is changing this default behavior, or maybe you are slightly altering the blank file before running the macro, and your co-worker isn't, etc.

A word of caution to avoid relying on ActiveWorkbook -- and especially in this case if the expectation is to always Set dwb to a new/blank workbook, the best way to do that is to explicitly create a new/blank workbook, rather than relying on the user to manually open a new/blank target workbook.

Set dwb = Workbooks.Add

If, on the other hand dwb must be assigned to some other known/existing workbook, then you should be either providing the file path to an Open statement, or the workbook name to the Workbooks collection.

On a related note, it's almost never necessary to Activate a workbook, see here:

How to avoid using Select in Excel VBA macros

And further note: your variables aren't globally scoped, they're scoped only to the module using Dim statement. A public declaration uses the Public keyword, not the Dim keyword. Both module-scoped and global-scoped should be used with caution (Public moreso than module-scoped) and in most cases it's preferable to pass objects/variables by reference to dependent subs and functions:

How to make Excel VBA variables available to multiple macros?

Community
  • 1
  • 1
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • Welp, that's a property of excel I've never observed and this might be the case actually. I will try this out tomorrow in work and let you know if it works. – kshaq Apr 06 '17 at 17:00
  • This was actually the problem, thank you very much for the answer! Such a small overlook caused me so much problems I still cannot believe it. – kshaq Apr 07 '17 at 14:50
  • @kshaq excellent! Glad I could help, and of course if this has helped solve the problem, do consider to "Accept" the answer. Cheers. – David Zemens Apr 07 '17 at 14:54