VBA Shared workbook and Unshared workbook - excel

I tried to find the code to share workbook and unshared it with visual basic but I didn't find it, anyone know if its possible?
Another thing is shared workbooks when saved, update the workbook to all users...
the question is if I save it with visual basic code the workbook will update to another users?
I am coding an button that when clicked it (share the workbook > fill the cells > save and unshared it).

I certainly agree with pnuts and the link he provided: Shared Workbooks are horrible.
To respond to the question though, if you record a macro in Excel you will see code like the following when you share a workbook.
Sub Macro1()
Workbooks.Add
With ActiveWorkbook
.KeepChangeHistory = True
.ChangeHistoryDuration = 30
End With
ActiveWorkbook.SaveAs Filename:= _
"F:\Documents and Settings\student\My Documents\Book1.xlsx", FileFormat:= _
xlOpenXMLWorkbook, AccessMode:=xlShared
ActiveWorkbook.ExclusiveAccess
End Sub
(If you don't already know how to record a macro in Excel then I recommend that you take the time to find out - it is extremely useful, particularly when you are just starting with VBA.)
If you copy this code into the VB Editor and click on certain words (SaveAs in particular) and press F1 you will get into the Help system.
From this recorded macro I surmise that removing Shared from a workbook is just a case of using SaveAs with an AccessMode other than xlShared (or omitted). After all, this is the dialog/option that appears when we manually share or un-share a workbook.
But, to emphasize, I am not advocating the use of Shared Workbooks.

Related

VBA Runtime error 1004 opening a workbook with macros (Excel 365)

I have a VBA script which will unlock some protected cells in a collection of excel workbooks, and am getting that lovely elusive "1004" error. The workbook actually opens (so the path is valid), but comes up with this error somewhere afterwards, but still within the Workbooks.Open command.
Through various sources, I have tried various combinations of the following lines, but it always fails after the workbook has opened and before returning control to VBA (when stepping through). Interestingly, it opens all the macro windows in the VBA IDE, but it doesn't highlight anything to indicate what should be the problem.
Dim wb As Workbook
Application.AutomationSecurity = msoAutomationSecurityLow
Application.DisplayAlerts = False
Set wb = Workbooks.Open([[valid file name]], IgnoreReadOnlyRecommended:=True, Editable:=True, CorruptLoad:=xlNormalLoad, AddtoMRU:=False)
I'm not sure if its to do with macros and trusts, or with protected sheets, but apparently the application security line should have solved the first and CorruptLoad the second. I'm sure there's some little thing I've missed - I just can't find it.
I've looked through the Workbooks.Open documentation online but there's no reference to protected sheets (CorruptLoad was gleaned out of a couple of different posts on people having similar-but-different issues).
#p77u77n77k was definitely on the right track, and essentially answered the question. Ensuring that "Trust Access to the VBA Project Object Model" is on certainly worked. In the process of researching, I found this script which can programmatically turn it on, if needed. Just one for the toolkit:
Macro to Enable Trust

Copy data range from macro free workbook to another macro free workbook

I am new to VBA!
I have a workbook A that I use as a template for spinoff workbooks B, C, D, etc.
I made an error in formulas range A36:E37. I need to correct it in all the subsequently created workbooks, which can have any random name
I want to open the corrected master workbook A, and copy range from A to whateverworkbookname
Every time I use thisworkbook refrerence, it pastes the data to my personal macro workbook, same thing with activeworkbook.
I'm sure there's a simple solution, (like assigning a variable to the freshly opened workbook that needs fixing?) but I don't know how to do that.
Help is much appreciated!
Also of note, I am planning on manually opening the whaverworkbookname, then VBA unprotecting the sheet, copy paste function, protecting the sheet, saving, and closing the whateverworkbookname book when the macro completes, to be repeated with the rest of the incorrect workbooks.
If there is a smarter way to do this (which is probably way over my head) like applying a macro to all workbooks in a folder for instance, I would be interested in a point in the right direction to learn about it.
First a note:
ThisWorkbook always refers to the workbook the code is written in. This never changes.
ActiveWorkbook is the workbook that has focus (is on top). This changes easily with a mouse click on any workbook.
The issue is probably that you run the code from VBA editor. But if it is in your personal workbook and you run from the editor, then as soon as you are in the code your active workbook is the personal one because that is where your code is and if you click there to run the code it has focus.
You can check if the active workbook is the personal one
If ActiveWorkbook.Name = ThisWorkbook.Name Then
MsgBox "The active workbook is the personal one. Make sure to focus on the correct workbook."
Exit Sub
End If
'rest of your code
Create a button or link a ribbon button to launch your macro and use ActiveWorkbook in your code.

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

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.

Resources