Problem with VBA - workbooks.open. I can't open two files simultaneously.
Hi,
Maybe it's simply the way it's supposed to be, but it seems strange to me. My macro works fine as long as I open only one file in the background using workbooks.open. But when I'm opening the second file, then the first one just closes. Is it normal?
Nothing very complicated. First i Open database:
Set database = Workbooks.Open(Filename:="Y:\EME\LW\PL-PIA\AIR\AIRFREIGHT\Zlecenia transportowe\baza_danych_transp.xlsm", ReadOnly:=True)
Then, when I open CMR, the database switches off.
Set CMR = Workbooks.Open("Y:\EME\LW\PL-PIA\AIR\AIRFREIGHT\Zlecenia transportowe\CMR_bis.xlsm")
Do I need to create a new instance of excel application for this?
I'm sorry, but I think I know what was causing the problem... I had a macro embedded in CMR that on opening of the workbook was getting some data from database and then was closing it. I dead forgot about it... Thanks for help, switching the names of the worbooks did remind me about it...
Related
I am working with openpyxl to make changes to a file and then save it.
Among the lines that are used to this, the one that gives me trouble is:
wb = openpyxl.load_workbook(filename=sheet_loc, read_only=False,keep_vba=True)
I know for a fact that the "keep_vba" is a very dangerous parameter, because if you call it True when you are saving a .xlsx file, the file will get corrupted. And vice versa.
Now, this is exatcly what happened to me. I had a .xlsx file and I wrote "keep_vba=True", so the workbook is corrupted. This is what I receive when I'm trying to open it:
My question is: Is there any way to fix that ? To recover this sheet ? I've tried "Open and repair" button, but it didn't work, it just gives me the same error said above.
I was having this same problem and I got the solution today.
To clarify, what you did here with openpyxl was nothing more than "turning" vba on for this workbook, but since the workbook still is a .xlsx file, it does not support this kind of thing.
To recover this file, you just have to change the type of the file to .xlsm, you can do this on windows by simply renaming it from "workbook.xlsx" to "workbook.xlsm".
I am opening an excel file in my code in 2 different places, with basically the same code. However, in one place it takes about 5 seconds and in the other 5+ MINUTES. I have tried opening in read-only mode in second place but no difference. Screen updating, calculation modes, events etc setting are all set to optimum and same for both procedures. Any ideas?
Procedure OK code:
Workbooks.Open Filename:=wFolder + "\" + lstVals(i)
Procedure Not-OK code:
Workbooks.Open Filename:=wFolder + "\" + lstVals(i)
Both procedures are also similar, taking in same input parameters, and are being invoked from the same form. Both are public Subs. Both open the file inside an if condition and a Do While Loop.
#OK basically opens this file as well as another, and adds the other file content to this file.
#Not-OK opens this file, does some editing to create a new excel which I save.
Time difference is in opening of the file, I have put in print statements before and after both invocations to narrow it down. I have also tried invoking both procedures in the same run, separately, one before/after the other, with other similar (different date and data) input files. Nothing makes any difference.
Have you tried to add a line like this:
DoEvents
before each invocation? In one case there may be some uncompleted actions that need to occur before the workbook can be opened.
Also, I'm not a big fan of Workbooks.Open, I would prefer:
Dim wb as Workbook
set wb = ThisWorkbook.Application.Workbooks.Open …
This way you are explicitly setting an object to the workbook you are opening and can then reference that object when reading/writing to the workbook. I've had troubles with other peoples' code when it was hard to decide which workbook was being referenced, which worksheet was the 'Active' sheet, etc.
The delay could be a result of the app pointing to a different active workbook or sheet in each case. By explicitly associating the open workbook with and object you eliminate the ambiguity.
I found the solution, though still cannot understand what the problem is.
High level flow of both procedure is similar (to the WB open point) - define vars, turn off updates, assign initial values, start loop, EXTRA processing for NOT-OK, open file, process...
EXTRA processing is where I get a region-to-process-for as user input. I don't need this region for the OK procedure. Also, I need to grab the region separately for each file I process in NOT-OK procedure.
If I move the region-capture dialog AFTER the file open, everything works fine. File opens quickly, then I get the region and process.
Anyone has any idea why a simple dialog box based capture should cause file open to take long? Also, I am capturing this region for file processing in other procedures as well. It doesn't seem to be causing problems anywhere else.
Thanks to everyone who helped! My apologies if I should have mentioned the dialog box at the outset. It is a simple InputBox call which I use in multiple places and I didn't think it made a difference.
In order to debug and calibrate different things, I would like to open the workbook at the beginning of the process i.e.
wb = xlsxwriter.Workbook()
I also tried with the with statement:
with xlsxwriter.Workbook() as wb:
But I didn't find anything which allow me to open the excel-sheet before the close method. I am pretty sure that there is an option for that.
Many thanks in advance for your help.
Is it possible to open and see the Workbook before the close method?
No. The file isn't created until you call close (either explicitly with close() or implicitly when using with).
first time posting but have learned so much in this place already! Now there's an issue for which i do not find a way out..
I have an excel sheet with a bunch of userforms which is used for invoicing. In one of the userforms, the user has the possibility to print the invoices.
All code runs fine up to the line:
fname = Application.GetSaveAsFilename("", "PDF Files (*.pdf), *.pdf")
Then the computer freezes, and Microsoft Excel is apparently using >60% of the processor capacities. After many minutes the "save as" window finally pops open, and all works perfectly again.
I've tried going around this issue and printing to pdf instead of saving to pdf, but the same happens on the line:
If Application.Dialogs(xlDialogPrinterSetup).Show = True Then
ActiveWindow.ActiveSheet.PrintOut
End If
Is there any known issue with this code on weaker computers? All code works fine on my computer, but not on the user's pc.
I've built in stops, and msgboxes on the other parts of the code, just in case the issue would be elsewhere, but it's really only on these lines that the computer panics.
Thanks a lot in advance for any advice!
OK, so far I've uninstalled & re installed Office-2010 3 to 4 times, done hours of research for 3 days with no success. I started getting this error either from Vbscript or Excel VBA, performing the same command that has worked for months. I am not sure what I may have changed, I don't recall changing anything that could cause it but I nailed it down to the ReadOnly:=True parameter. I think something may be wrong with the registry... but re-installing should fix it right? It didn't...
Anyways, very simple command that I recorded on Excel 2010. This command WORKS FINE and the file opens:
Sub Macro1()
Workbooks.Open Filename:="C:\temp\file_9928_131101.xlsx"
End Sub
But when I add the ReadOnly:=True parameter, it does NOT WORK:
Sub Macro1()
Workbooks.Open Filename:="C:\temp\file_9928_131101.xlsx", ReadOnly:=True
End Sub
This is the returned error Run-time error '1004' Method 'Open' of object 'Workbooks' failed:
When I click Debug, the error is at the only line of code.
The file is corrupted. Resave it with another name and change the name in the function. Try that it works and after that rename the file as you want to call it.
It worked for me and I had a corrupted file. The read only shouldn't be a problem.
Putting an answer here for others like myself who have this issue and the normal solutions don't work.
Another potential cause of this is corrupted temporary files. I think it may only apply if the file you are trying to open is on a network drive or other remote host.
Anyway, try wiping your temp folder (as in the one you get to if you type %temp% into Windows Explorer) then restarting the computer.
I have had the same issue with an Access file stored in a local OneDrive folder, referencing an Excel file stored in the same local OneDrive folder. The solution was to move all files into a "static" (i.e., not synchronized, not OneDrive) folder.
Thought that this specific case/application might help someone.
I realise this is late but if you want to open & repair a corrupted workbook automatically use:
Set oWB = Workbooks.Open(Filename:="C:\my\file\path.xlsx", CorruptLoad:=XlCorruptLoad.xlRepairFile)
If you are downloading the file from some external source (eg- email), just open the file directly from the email and then save the file. Try opening the file in macro ...hope it works...it worked for me....:)
I had a rogue excel process that was running in the background. When I killed it from the task manager the code worked. I hope this helps.
I was too, going crazy with Workbook.Open function, getting the weird Open fail error for no obvious reason. My code also did some some copy-pasting after Open function, which seemed to trigger it.
Eventually I found out that turning ScreenUpdating OFF seemed to cause this - keeping ScreenUpdating ON, made things click for me (maybe someone finds this helpful in the future). I'm one of those who tends to switch ScreenUpdating off for all code whenever possible.
Application.ScreenUpdating = False
I know I'm answering late on this, but I resolved a similar issue (same error but running excel from a .Net app) by making sure VBA was installed correctly on the target machine.
Control Panel->Programs and Features->Uninstall a Program... find your Office install, Right-click and select "change" ->Add Remove Features->Office Shared Features->Visual Basic for Applications->Run From My Computer
This did it for me.