Excel: problem with deleting .Connections in a protected workbook - excel

While trying to remove external connections from my protected Workbook (which imports data from a csv file) with VBA, I run into Runtime error '5'. Strangely, it works if I unprotect the Workbook.
I found this thread: Protect Excel Worksheet For Read Only But Enable External Data Refresh, but the proposed solution of unprotecting the workbook while the scripts are running is out of the question.
Here is the code that I use to remove the external data connections:
Sub RemoveExternalDataConnections()
Dim i As Long
For i = ActiveWorkbook.Connections.Count To 1 Step -1
ActiveWorkbook.Connections.Item(i).Delete
Next
End Sub
All in one, I would like the external data connections be removed even if the workbook is protected.

For anyone else - it seems that unprotecting the workbook is the easiest way. Setting up a password on a document/VBA project is not particularly safe anyway and it will not block most tech savvy users from accessing it.

Related

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.

How to protect a specific workbook only in Excel VBA

What is the best way of protecting a specific Excel workbook?
I have an inherited script that includes the following common lines at the end:
ActiveSheet.Protect "my-password"
ActiveWorkbook.Protect "my-password"
However, I've noticed that as the script can take a few minutes to run users often switch to a new unrelated workbook whilst it solves - whatever else they are working on. The password protection is then inherited by the unrelated workbook upon the completion of the macro - since whatever other Excel file the user is working within is now "Active" (presumably? this is my reading of the problem).
The above script is in a workbook that can be renamed to whatever the user chooses, and be saved in any number of directories. How can I ensure that only the original excel file is locked/unlocked by the Macro, when other workbooks are in use?
I am sure there are many ways to do this, but which is the most foolproof method?
NOTE: using office 365
Thanks Dean's answers in the comments:
Early in the code (and in Worksheet_Change if appropriate) enter the following to define your sheet as an object (named default_ws in my case):
Set default_ws = ActiveSheet
When you are ready to lock your sheet or workbook you can then use:
default_ws.Protect "password-here" 'protect your sheet
ThisWorkbook.Protect "password-here" 'protect your workbook
Also note:
You could also define your workbook as an object as follows if desired:
Set default_wb = ActiveWorkbook

ThisWorkbook.Protect and Unprotect in Close and Open events

I want to unprotect a workbook immediately upon opening it, if it is protected the last time it was closed. I place the following code at the very beginning of the Open event:
If ThisWorkbook.ProtectStructure = True Then
Call ThisWorkbook.Unprotect("openpassword")
End If
Similarly, I want to protect the workbook right before it is closed. So I place the following codes at the very end of the beforeClose event:
If ThisWorkbook.ProtectStructure = False Then
Call ThisWorkbook.Protect("openpassword",True, False)
End If
ThisWorkbook.Save
Neither of those two works! Meaning, if a workbook is already protected upon opening, none of the code in Open event is executed. Not even the unprotect call!
Similarly, in the beforeClose event, all codes before the protection part are executed, except the protection part and any codes following it. I have confirmed that by placing it in different places in the beforeClose event.
Anyone could help me with this?
Additional explanation why I want to do this: the workbook is being shared among users with different level of authorized access. Most users face a protected workbook when allowed to work on only certain sheets. Hence, depending on who uses it last, the workbook could be closed in a protected state or unprotected state. If it is unprotected during the usage, I need to protect it immediately upon closing, so that the next unauthorized users will open a protected workbook.
Can you check that the events are actually firing? Try putting a Stop at the beginning of each event handler to see they are called. If the code isn't executing, what happens if you save it as a trusted document?
Just a few things:
Why don't you force a protection when closing for everyone, and only allow unprotecting when opening for certain people?
How are you determining which users can unprotect the workbook?
You're not saving the workbook as a shared workbook, right? It's a normal workbook, but accessed by different users. Shared workbooks are a bit funny with protection.
Try the following instead:
Dim sht As Worksheet
For Each sht in ActiveWorkbook.worksheets
If sht.Protection = True Then
sht.Unprotect("openpassword")
End If
Next sht
And the same at the end:
Dim sht as Worksheet
For Each sht in ActiveWorkbook.Worksheets
If sht.Protection = False Then
sht.Protect ("openpassword", True, False)
End If
Next sht
In excel - as far as I know - it isn't possible to protect the whole workbook directly but instead you have to protect every sheet.
You can at least try it, if it works I'm happy too :)
CU Kath

Run macro in shared workbook and protect it

I want to run a macro in a shared workbook. My macro as below.
Sheets("Sheet1").Unprotect
Range("A1").Select
ActiveCell.FormulaR1C1 = "ss"
Sheets("Sheet1").Protect
When I run the macro without sharing it works fine. When share it below error prompts:
Unprotect method of Worksheet class failed.
Can anyone help to run my macro in shared environment and keep the sheet in protection?
This is a limitation of Excel.
Sheets can't be protected or unprotected whilst shared. No way around it short of changing Excel itself. Afraid there's no other way.
You'll also notice that you can't manually unprotect the sheet whilst it is shared as well.

Excel 2010: How to change pivot table source data without disconnecting slicers?

I have researched like mad about this, and I'm worried there isn't an answer. But maybe the really smart people on this site can help.
I have two workbooks that work together - Charts.xlsm and Data.xlsm. They are always kept together in the same folder. The Charts.xlsm obviously contains all of my charts, but they are all linked to tables in Data.xlsm for their source. I also have lots of slicers in my Charts.xlsm that are connected to the charts, and they share caches when they are connected to charts with the same data source. The two workbooks are always open at the same time so that the data source reference looks like this: 'Data.xlsm'!Table1
This all works great, until I put these workbooks on another computer (which is why I am doing this so I need to find out how to fix this).
Once the workbooks are closed, the source data references change to a specific location on my harddrive: 'C:\Folder\Data.xlsm'!Table1
If I want to manually change this back to a local reference, I have to first go through and disconnect every single slicer, refresh the tables, then reconnect every slicer. Not a viable solution for my clients.
I would use VBA to change the references every time Charts.xlsm is open, but when I tried it one of two things would happen: either the workbook produced errors that would prevent saving, or Excel would crash completely.
This is the code that works perfectly for disconnecting the slicers, but produces the 'save' error:
Sub Disconnect_Slicers()
Dim oSliceCache As SlicerCache
Dim PT As PivotTable
Dim i As Long
For Each oSliceCache In ThisWorkbook.SlicerCaches
With ActiveWorkbook.SlicerCaches(oSliceCache.Name).PivotTables
For i = .Count To 1 Step -1
.RemovePivotTable (.Item(i))
Next i
End With
Next oSliceCache
End Sub
So... I am asking the Excel/VBA geniuses out there if there is any way I can maintain a relative location for my charts when they are looking for Data.xlsm so that no matter what computer I open those workbooks on, they will always be actively linked.
Thank you SO much in advance!
If always both files are in the same folder you could possibly go this way.
A. Switch off auto 'UpdateLinks' of Chart.xlsm file. You could do this once manually or, for safety reason, always when BeforeClose event fires to avoid some possible problems:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.UpdateLinks = xlUpdateLinksNever
End Sub
B. When you open Chart.xlsm change the link to Data.Xlsm using Workbook Open event + additionally refresh links. In this situation we check path to Chart.Xlsm file and search Data.Xlsm in the same folder. I assume that there is only one link to any other file otherwise some changes could be required:
Private Sub Workbook_Open()
'changing first and only one link to new one
Dim a
a = ActiveWorkbook.LinkSources
ThisWorkbook.ChangeLink Name:=a(1), _
NewName:=ThisWorkbook.Path & "\Data.xlsm", Type:=xlExcelLinks
'update the link
ThisWorkbook.UpdateLink Name:=a(1), Type:=xlExcelLinks
End Sub
I admit I do not consider all the risks therefore some test are required.

Resources