Deleting Named Ranges with Excel VBA Corrupting File - excel

I'm running into a situation where as soon as I run the following lines of code, my file gets corrupted upon close and re-open.
Dim MyName As Name
For Each MyName In Names
ActiveWorkbook.Names(MyName.Name).Delete
Next
I had also tried to replace the code above with the following, and get the same impact:
Do While CBool(ActiveWorkbook.Names.Count)
ActiveWorkbook.Names(1).Delete
Loop
The error upon re-opening of the file is as follows:
"We found a problem with some content in 'File X.xlsm'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes'.
After this, the file repairs the file by 'repairing or removing the unreadable content' with message 'Removed Feature: External data range from /xl/worksheets/sheet7.xml part'
Any ideas what's going on here? I'm glad I was able to isolate the code causing the issue, but I'm at a loss. I'm using Excel for Office 365 MSO 32-bit.

You do not define the collection that Names belongs to. Try modifying to define the collection of names.
Dim MyName As Name
For Each MyName In ActiveWorkbook.Names
MyName.Delete
Next

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.

Excel 2016 office 365 catastrophic failure

Start to get Excel catastrophic failure error
On OK opening debug windows, with auto creating each time new sheets, which is empty and strange structure
If I want something to do appears
So how to delete those sheets? or fix that error?
No background process started, file stored in xlsm and xlsb format do the same things. workbook and worksheets is not protected.
It looks like the file has been corrupted. It is unlikelly the problem can be easily reproduced from scratch.
Never the less you can script a vba macro to delete Sheets based on their names or not delete the sheets you want to keep.
sheetnametodelete= "sheetname"
With Application.Workbooks(ThisWorkbook.Name())
.Unprotect (yourpassword) ' required if protection is set
Dim wks As Worksheet
Set wks = .Sheets(sheetnametodelete)
If (Not wks Is Nothing) Then ' also check if wks belong to the defined blacklist
wks.Delete
End If
.Protect (yourpassword) ' required if protection is set
End With
Try to open the file from another computer in case your local Excel config is corrupted.
I had a similar problem (a fake workbook duplicated) in the past and decided to script a build process for my Excel vba based application.
See following links to learn more about module management.
https://www.rondebruin.nl/win/s9/win002.htm
http://www.cpearson.com/excel/vbe.aspx
you can also look at this post
Import a cls files and create a sheet
It provides code and comments from other contributors.
This is obviously not direct answer to your problem but if you intend to work on a consistent vba project I recommand to save your vba code out of your Excel file once in a while and setup a build of your Excel app.

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

VBA Run-Time error '9' (Subscript out of range) on transfered in worksheet

This is my first post and I new to programming in VBA. I have a large excel file where I am trying to create a macro to delete multiple tabs. My code is as followed it works as I am prompted with a do I wish to permanently delete:
Option Explicit
Sub delSheet()
Worksheets("sheet2").Delete
End Sub
However when I transfer in a tab (tab name is "sheet92') from another file I get this error using the same code
Option Explicit
Sub delSheet()
Worksheets("sheet92").Delete
End Sub
What is causing the code to delete the sheet2 which is the tab that I created within opening a .xlsb file and creating a new sheet vs. the error message on copying into the file from an existing file? Thank you advance for your help.
Your question is not very clear, but you probably have to reference the correct workbook, like this:
Workbooks("file.xlsx").Worksheets("sheet92").Delete
If the workbook is not already active, giving only Worksheets("sheet92") won't work.
I solved my first progroamming bug! I was using the wrong name, I was using the field (name) vs. the actual name of the tab, I took a screen capture here !http://imgur.com/a/bPNkg! once I used name Sheet1 I was able to delete. Thank you for your help #cub

Error when setting workbook variable - 2147352565

I have a simple code that runs upon initialization of a userform that sets a few workbook and worksheet variables so they can be used easily throughout the rest of my modules, and the references can be easily changed in one place if the file moves. I recently migrated my workbooks from my desktop to a separate server/drive, and accordingly updated the file pathways; however, when I try to run the code now I immediately get the message:
"Run-time error '-2147352565 (8002000b)': Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus."
This error occurs on the line
Set ReportWkbk = Workbooks("N:\ rest of file pathway here\QuaRT_Template.xlsm")
Is there anything that can be happening on the server/drive that is causing this issue? If so, is there anything I can do to fix it? I do work with other excel workbooks saved in the same location that seem to have no issue being referenced, though their references are in the workbook itself, not through Visual Basic.
The Subscript Out of Range error occurs because the Excel workbook being referenced is not open (or opening) in the same instance of Excel. You can easily reproduce the error by creating two workbooks (name one WorkBook2.xlsx) and ensuring they open in separate instances of Excel. Then run this code:
Sub OpenWkbkNames()
Dim wbk As Workbook
For Each wbk In Workbooks
Debug.Print wbk.Name
Next
'Hmm. Workbook 2 is not listed, but lets activate it and see what happens.
Workbooks("workbook2.xlsx").Activate
End Sub
You could avoid the issue by looping through the names of the workbooks open in THIS instance of Excel to ensure its available.
I've been researching Run-time error '-2147352565 (8002000b)' and found this: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vsconstants.disp_e_badindex.aspx
Apparently, that is VS's way of saying Subscript Out of Range too. I suspect (but have no solid proof) that since the code worked locally but the issue appeared after the file was migrated to a shared drive that Windows is taking a long time to open the file so opens it in a new instance of Excel. Essentially, Excel gets impatient and decides to move on while Windows completes its task. I base this assumption on the detection logic that was added to Excel 2013 (https://blogs.office.com/2013/06/03/opening-workbooks-by-running-separate-instances-of-excel/).

Resources