Excel VBA suddenly not able to open workbooks? - excel

My problem is very strange.
I was programming a macro to open workbooks and extract data which was going fine when it hung one time. I had to end process in task manager and restarted excel.
Now even basic file opening code doesn't work.
My basic code is like this
File path is C:/ and fileName is Book1
Set wbOpen (filePath & fileName)
wbopen.activeSheet.Range ("A1") = "Test"
.close SaveChanges:= True
Msgbox "ok"
.... end sub
The code used to work and i would see book1 being opened on the taskbar and the cell A1 will be changed. Now i still get the Ok message but the cells arent changed and i dont see book1 being opened. Any idea? Pls and thank you
Tried it on another PC and it doesnt work, could be excel settings
Already reset settings using the /regserver method and deleted the .vbe files under AppData too.

Sorry, was a very stupid mistake, must have accidentally set file path to "C:/Excel" instead of "C:/Excel/"
Tracked it down by using the msg box to print out the file im trying to access

Related

How does Excel know that a file "was not recalculated before it was last saved". Can I trick Excel into thinking the opposite?

When linking workbooks in Excel, I often get an error like:
Links to xxxx.xlsx were not updated because xxxx.xlsx was not recalculated before it was last saved
This error pops up once for every linked value, which means in my case about 100 alerts I need to press OK for. Mysteriously, this alert comes even if xxxx.xlsx contains no formulas and hence no recalculation at all: it's completely full of values only.
So how does Excel know that a file has not been recalculated before saving? Is it looking at a particular xml value inside the ZIP file (xlsx) which I could tamper with? Is it looking at open date vs modified date that I could circumvent with the touch linux command? I'd like a solution Using the command line ubuntu if possible (I run windows WSL), so that I can use a script.
And what's more, xxxx.xlsx is really big, which over network (thanks COVID) at home is slow to open / recalc / save. So I really don't want to ever open this file in Excel.
Any ideas?
You could try adding this macro to your PERSONAL.XLSB file and then running it. It will ask you to select a file and then open it without allowing links to update.
Sub OpenWithoutUpdatingLinks()
Dim strFileName As String
strFileName = Application.GetOpenFilename
If strFileName <> "" Then Workbooks.Open FileName:=strFileName, UpdateLinks:=False
End Sub
This will allow you to open the file you're working on without getting the message about updating links.
However, if you actually need the links to update or need to create more links, then you need the linked file to be recalculated.
Let us know if you need instructions on adding a macro to your personal file and running it.

Subscript out of range - Can't find Open Workbooks on Another Computer

I am a newer user of Excel VBA. I have been checking through all the other questions on here to try to solve my problem. I created a Master workbook, which copies info from 3 template spreadsheets when I run the macro. On my computer, it works correctly. However, when we tried to run it on a coworker's computer with the exact same 4 workbooks open, we ran into the "Subscript out of range" error.
I used the below code from this post (Out of range error for Windows().Activate) to check the names of all the open workbooks. Only 1 of the 4 (Master.xlsm) open workbooks displayed when I ran the code on my coworker's computer. On my computer, it displays 4 out of 4 open workbooks when I run that code
dim oBook as workbook
for each obook in workbooks
debug.print ">" & obook.name & "<"
next
Below are the first lines in my code. It breaks on the first line because it can't find the template workbook even though we have it open. When we run the above code on my coworker's computer during debugging, the Template1.xlsx workbook is not listed. I am not sure what is going wrong, and would really appreciate any help on this.
Sub Copy_To_Master()
Workbooks("Template1.xlsx").Worksheets("1").Range("A65:E104").Copy
Workbooks("Master.xlsm").Worksheets("1").Range("A65:E104").PasteSpecial Paste:=xlPasteValues
End Sub
I'm guessing that your workbooks are opened in different instances. You can't control what your users are going to do so I would recommend opening the files using VBA so that you get a handle on them as they are opened.
Dim templateBook as Workbook
Set templateBook = Workbooks.Open("Template1.xlsx")
This way you can use the templateBook object to do all the things you need to do. I would then further recommend that you keep these workbooks on a server share so their location is reliable. Then any employee can run these things, they all run off the server and you never have to worry about "works on my machine" again.
Finally, don't use copy and paste. Once you have workbook handles you can set values directly without copy and paste. It looks like this:
masterBook.Worksheets("1").Range("A65:E104").Value = templateBook.Worksheets("1").Range("A65:E104").Value

VBA Workbooks.Open crashes excel

Ok here is the issue, i need to open a large workbook from another large workbook, it was working fine till now, and i've not changed it, here is the thing it suddenly started crashing when the full path is on a speciffic location.
the address is fine, the password is fine but then when i get to this line:
Workbooks.Open FileName:=PROJECT_DETAILS_WB_FULL_PATH, UpdateLinks:=True, Password:=PROJECT_DETAILS_DECRIPTION_KEY, ReadOnly:=False 'here is the problem
and excel crashes entirely
-Even while running step by step so the "wait" method isn't it
-I have the same issue with office 2013 and 365 so not an office version issue
-Also tried in different computers and the issue persists.
-Replaced the target file with one that 100% works and still.
-if i open the file manually it works (there is a check for the file been already open)
My guess is that it is a folder permit or file permit issue on the target path, if anyone know what I should check for that would be helpfull
Ok guys and girls, thanks for your help, after poking around all day I found the issue, in summary, file "A" was trying to open file "B" and excel crashed, turns out that file "B" has links to another file, file "C", which was not up to date, those links are to named ranges that didn't exist in the older version of file "C".
When file "B" attempted to check (before "UpdateLinks:=True") excel crashed, updating file "C" to the latest version worked. Note that when prompted to update the links manually, Excel does not crash regardless of what you choose.
Try the following code.
Sub openwb()
Dim wkbk As Workbook
Dim NewFile As Variant
NewFile = Application.GetOpenFilename("microsoft excel files (*.xlsm*), *.xlsm*")
If NewFile <> False Then
Set wkbk = Workbooks.Open(NewFile)
End If
End Sub

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