Excel Workbook open macro reopens closed workbook - excel

I do the following as a macro
Open a list of files
copy some values
Close them
After that when I exit and reopen the file that contains the macro, it also opens the files which I previously opened. even those I had used the app.workbook.close
I'm unable to find the problem out.

Where is the macro located? In a normal module?
At the end, seeing as how you've already pointed the variable at it, you may as well say
currentWB.close False
Then to close
Set currentWB = Nothing
Are there some links between the file that has the macro and the files it creates?

Related

VBA can't open a workbook that exists

I have a loop in VBA that takes a file path from a list, attempts to open that file, and checks to see if that file has external links. I have catches for if the file is corrupt, if it is password protected, and if you need to enable editing when the code initially opens the workbook. The loop works until it gets to one particular file. Whenever the code reaches it, it pauses for about 5 seconds and then closes the workbook that has the macro running in it. I'm able to copy the file path and paste in the file explorer, so I know this particular workbook exists. Here's the start of the loop, with the rest of the code being the error catches and what to do when the code detects external links. I've tried starting the macro at this file's index number (StartNum) on the list and going line-by-line, so I'm pretty sure the problem comes at this point.
'''
Do While StartNum <= EndNum
'at this point in the loop, the activeworkbook is set to be the excel workbook running the macro
ActiveWorkbook.Save
'first error catch. ActiveFilePath is the full file pathway of the workbook I'm trying to open
On Error GoTo CorruptFile
Workbooks.Open ActiveFilePath, UpdateLinks:=0, ReadOnly:=False, Password:=""
'''
Thanks!

Excel VBA: Workbooks.Open(absolute_path) freezes Excel, except under one condition

Under this line of code, Workbooks.Open(previous_absolute_path) freezes Excel most of the time, giving no error message:
Set current_workbook = Workbooks(get_file_name(current_absolute_path))
The surounding code:
If IsWorkBookOpen((current_absolute_path)) = False Then
If previous_absolute_path <> "" And previous_absolute_path <> current_absolute_path Then
Workbooks(get_file_name(previous_absolute_path)).Close True
End If
Set current_workbook = Workbooks.Open(current_absolute_path)
previous_absolute_path = current_absolute_path
Else
Set current_workbook = Workbooks(get_file_name(current_absolute_path)) 'Causes Fail!
End If
current_absolute_path is a valid file path that is absolute. It seems every time the code runs there are windows at least being opened. However, in the newly opened workbook, no lines of code are ever executed in itsworkbook_open() from what I can tell from putting a message box as the first line of code ofworkbook_open() and the message box never appears.
The code does work, if and only if I have the workbook running this macro open, and before I run that macro, I manually open the workbook at current_absolute_path. I close that file and run the macro and it works without any problems.
current_absolute_path, is a confirmed valid file path. The line of code can open other workbooks within the same folder. Excel allows all macros and "trusts" files from the internet at this point. The folder of the workbook being opened is not protected.. and I do not have the skills yet to know where the problem is. What must be done?
EDIT: It's not manually opening the workbook that causes the Excel macro to run successfully the next time, specifically its manually closing it.

Excel 2016 sharepoint file still opens as readonly

Since updating to Office 2016 I can't get excel to open a sharepoint file as editable, despite declaring it to do so.
Workbooks.Open ThisWorkbook.Sheets("Filelist").Cells(i, 2), _
UpdateLinks:=False, ReadOnly:=False, Local:=True, Editable:=True
The file opens without issues, but I have to run a break on the next line to stop the macro and manually select EDIT, before allowing the code to proceed.
Whilst this is an obvious work around, I am looping through about 40 files, and have to do this manually in each open instance.
Ok so I found a solution to this LockServerFile is the equivalent of hitting the Edit Workbook button.
When opening with VBA you can follow the open command with:
Workbooks.Open ThisWorkbook.Sheets("Filelist").Cells(i, 2)
ActiveWorkbook.LockServerFile
Solved my problem for now if anyone comes across a similar issue.
ActiveWorkbook.LockServerFile
The above code will lock that workbook for editing.
You can edit the workbook and you can save it.
But when you try to open the workbook again by manually, the changes you had done will not reflect in that sheet.

Activating Macro view button sends excel in non-responding mode

In Excel 2013: macro was working fine until today. Now, I can open the "Personal.xlsb" file, but I cannot use the Macro view button anymore, all I get is Excel in unresponding mode; and then, only way is to stop Excel entirely. When I go in design mode, I cannot access the macro registry at all (trying to do so results in the same unresponding state). I rebooted the PC entirely, did not help.
Pb seems specific to one specific "Personal.xlsb" file (I replaced the incriminated file with another "Personal" file in the XSTART folder and Excel macros worked just fine with a different .xlsb file). So I am suspecting a file corruption pb. If that is the case, is it possible to recover the original macros, or at least crack the macro file open and get a copy of the original coding?
You can try to get back your code if you manage to open the workbook from a macro in another workbook. Give this a shot:
create a folder where you will get the recovered code modules. Let's say "C:\myRecoveredCode". Put in a copy of your corrupt file "Personal.xlsb"
In Excel Options, Trust Center Settings, Check Trust Access to the VBA project object module
create a fresh workbook, copy/paste and run the following macro:
Sub TryRecovery()
myFolder = "C:\myRecoveredCode\"
Set wb = CreateObject(myFolder & "Personal.xlsb")
For Each comp In wb.VBProject.VBComponents
comp.Export myFolder & comp.Name
Next
wb.Close False
End Sub
If all goes well, you will have files a set of files that you can edit, or import to another workbook.

Old workbook (once opened by VBA) keeps opening when I open another excel file

I am building a database in Access, for which I import data from an Excel workbook questionnaire. I have coded an Import-sub that selects, opens, retrieves the data from and finally closes the workbook.
The problem is this: for some reason, when I now open any excel workbook on my computer (at a time when neither Access or Excel is in use) some old version of the questionnaire keeps opening as well. This problem doesn't end by restarting the computer, but only by deleting that specific questionnaire-workbook. But then it starts happening with another workbook.
I have a theory that this might be because I - in my import-sub - have opened the questionnaire, encountered a run-time error which has ended the sub before it closed the workbook, and that somehow the workbook is still "open". Or that there still is a link active.
So I have two questions:
1.) Does anyone know how I can fix this problem?
2.) Is there generally any consequences of not closing a workbook that you open through VBA?
My relevant code is:
Dim MyXL As Excel.Application
Dim MyWB As Excel.Workbook
...
in between lots of stuff happening, several times an error occurs which interrupts the program.
...
MyWB.Close False
MyXL.Quit
Appreciate any help on this!
I did Encounter the same Problem and found out that Excel stores the files that open whenever you start Excel in a Folder (XLSTART). The path to mine was: c:\USERS\MyUserName\AppData\Roaming\Microsoft\Excel\XLSTART
As suggested by Ross McConeghy error handling may prevent such an occurrence. But the error already happened and the questionnaire, as you suggested, has placed that workbook in the Folder XLSTART. You have to delete it from that folder to fix the unwanted occurrence.
Your theory is likely. If you never display the Excel application, errors are encountered, and your code never closes the workbook it would be left open in the background and your code would most likely lose reference to it. The next time you open an Excel file the instance of Excel that is already open(but hidden from view) "picks up" the request and opens the file, also displaying the file that was still open from the macro.
1.) You should set up error handling so that the Workbook and Excel application are closed when there is a non-recoverable error.
Sub MySub()
On Error GoTo handle_Err
<... code ...>
Exit Sub
handle_Err:
MyWB.Close False
MyXL.Quit
End Sub
If you have other Error handling statements in your code always reset to On Error GoTo handle_Err instead of GoTo 0 etc.
2.) The only consequences of not closing the Workbook are the obvious ones- the system resources it's using and, if it is open for editing, no one else can edit it.
I had a similar problem and solved it a different way.
I found the connection to an external workbook and fixed it by going to Data > Edit Links.

Resources