VBA not updating on Protected Sheet -- Excel Office 365 - excel

There are tons of questions already posted with a similar problem, but none of those solutions works for me. I'm using Office 365, so maybe it's something that changed there. Anyway, here is my issue.
I have a file that pulls info from one main tab into other tabs. On the subsequent tabs, I have VBA that auto-hides rows depending on the value that was pulled from the main tab. Anytime the data in the main tab is updated, the subsequent tabs should also update. I had an issue with making this work, but I worked it out by using another cell to store the previous value that had transferred (comparing them, and running the code any time they were dissimilar). The problem now is, of course, that it wont run when the tabs are protected. The tabs need to be protected to prevent end-users from corrupting the formulas and formatting, but I can't make it work with any of the solutions posted on StackOverflow. My code looks something like this:
Sub Worksheet_Calculate()
If Range("Old Value") <> Range("New Value").Value Then
Range("Old") = Range("New").Value
Rows.EntireRow.Hidden = False
<<Insert code to auto-hide based on "New Value" cell>>
End If
End Sub
I've tried adding Worksheet.Unprotect Password:= and Worksheet.Protect Password:= commands on either side of the If statement, and I get a 424 Object Required error. I've tried using the "UserInterfaceOnly = True" command with my password protection, and it changes nothing (still get the trying to change a protected sheet error). I've tried creating another subroutine for unprotecting and re-protecting, then using a GoTo to run the original subroutine; but that didn't work either. I see a lot of solutions using ActiveSheet commands, but I don't think I can use them. I'm updating the main tab, and the action is happening on another tab. I'm fairly new to coding, and I'm teaching myself as I go; so, I could be missing something obvious.
It works perfectly when unprotected, so I guess I may have to just leave it that way. I hate wasting time fixing the files when end-users do their best to ruin them, though, so I'd love to leave it protected.

This may not be the final answer, but I seem to have stumbled upon a solution. Since the only thing I want happening on the protected sheet is for rows to auto-hide, I left row formatting unprotected. It seems to work fine that way. You would think there's a way to run your VBA normally without having to jump through hoops because of protection....but, I'll take it for now.

Related

How to change Print Settings to `Print Active Sheets` at run time instead of Print Selection?

I need to change Print Settings to Print Active Sheets instead of Print Selection.
I tried using macro recorder, but it did not capture any code.
Also, this simple vba code just print the active sheet, but it does not change print settings as required at run time.
ActiveSheet.PrintOut
In advance, your help is appreciated.
The setting in discussion is workbook specific. You cannot change it globally even using another language, because of the previous mentioned truth. I mean, using a specific language method.
But you can do it, using VBA, simple running a piece of code like the following one printing two sheets. Next time, for that specific workbook the setting will be changed. You obtain what you need when the code runs and also change the setting:
Sub printActiveSheets()
Dim wb As Workbook
Set wb = ActiveWorkbook
Sheets(Array(wb.Sheets(1).name, wb.Sheets(2).name)).PrintOut
End Sub
It will print first and second sheet and will also make the change you need.
In order to check that, please manually print a selection, then look at that specific setting. It will be changed in 'Print Selection`. Then, run the above code and make the same check. You will see 'Print Active Sheets'. It will also be memorized for that specific workbook if you save it.
It is not easy to change that setting, let us say, As Default, being valid for all open workbooks. I cannot swear that it is impossible. If I will take it like a challenge I will maybe find some Registry keys able to be handled (as Excel does) in order to do it for all involved workbooks. But the effort cannot be covered by the result, I think. This is the way Excel works, should be the essence of the answer, I also think.

Run a macro in an AddIn from a button on an Excel sheet [duplicate]

This question already has an answer here:
Can I assign a function in an excel add-in to a button in the workbook?
(1 answer)
Closed 3 years ago.
I would like to run a macro defined in an AddIn from a button on an excel sheet. I tried to enter the name of the macro in the "assign macro" dialog box that appears when right clicking the button (I don't know the exact English title of the dialog as I'm working with a German Excel version) in the formats
AddInName!MacroName and
'AddInName'!MacroName.
In both cases the name of the AddIn is stripped away automatically when I reopen the dialog.
The format "'AddInName'!MacroName" was rejected as "a too complex formula"
On the machine on that I am developing the AddIn this works but on another machine where the AddIn is running without problems this particular function does not work.
Is there a way to access a macro inside of an AddIn that may be located at different locations on different machines (C:/user/"user_name".../AddIns) ?
Long story, made very [long]: maybe.
You'll have to work out the file sharing and permissions between the two computers but you can use an AddIn on a shared network drive so it is entirely possible.
I would caution against using the same AddIn simultaneously between computers. It would be easier, safer, and more stable to make a copy.
You're on the right track with your string creation, but rather than adding it in the macro selection box, add it to the OnAction property of the button in the worksheet's code module.
I'm going from memory so I might be mistaken but I'm pretty sure your string will be built like this...
"'[addin.xlam]!project.module.macro'" \edit: tested and confirmed not to work
Note that there are there ticks, it is an outer "double quote" encapsulating an inner 'single quote'
You can also pass arguments to the macro with an appropriately built string too, it's relatively easy when using variables or integers but it gets tricky when you start using string literals.
Just finished my post to read this:
"#Peh: That might work, but then the excel sheet has to be an xlsm file what I wanted to avoid. – chrmue 9 mins ago"
Obviously that rules out my entire solution.
I spent several hours on this and all I've done is run around in circles.
The comments in my code appear to be wrong, and while I all passing parameters in my working code, I am only doing it within the same workbook and have failed every attempt to send them to another workbook via OnAction and even Application.Run
It may be a fools errand but I'm convinced it's possible so in but giving up.
I have a button on the sheet:
Shapes.Add xlButtonControl
It passes two arguments through a procedure:
OnAction = "'passthroughme ""passing"", ""through"" '"
But hithertofor, that's as far as it goes. No success reaching external workbook's while also including arguments.
I also have several examples of existing code that works just fine, but I haven't been able to get any is them to pass arguments to an external workbook:
Select Case Application.Run(testingFunction)
Application.Run procToCall, procArgs
testResult = Application.Run(testingFunction, testingArgs)
Application.Run "extractPDF." & loPDFvendors.ListColums(gTCN_PDFMAP_PROCLIST).DataBodyRange(mSupplierIndex), ws
Not giving up yet, but I am defeated for the moment

How to Prevent Free Distribution of Excel Spreadsheet

I reviewed the answer to the question at the link below. This seems to match my need, but I am not able to get it to work for me. What am I doing wrong so this will work. This code can be a great tool for me to limit the free distribution of my spreadsheet which I have commercialized.
Thank you for the help.
How to prevent free distribution of a commercial Excel spreadsheet
When I put the code into the modules and save the file, when the file is opened I am receiving the following error.
Compile Error:
Sub or Function not defined
It then highlights the reference to "Refresh_Serials".
I am running this currently on Excel 2007. Do I need to try it on a newer version of Excel maybe?
I have used the same code as included in the link below.
I would expect it to do as expected per the original post, although I am not 100% sure exactly what the outcome should be. Maybe part of my gap at this point is that I haven't yet established the HTML table. Could I use a "Google Sheet" instead with a table in it?
If you want this Workbook to be compatible for Specific Users only. Then one way to achieve it is to add a WorkBook_Open event. Like:
Private Sub Workbook_Open()
If Not Application.UserName = "Mohit Bansal" Then
MsgBox "Not the Correct User"
ThisWorkbook.Close False
End If
End Sub
Change the Name as Per the User, and lock the VBA Project. You can even make a list of names to check from if multiple users are going to use it.
This will make sure that only one user is able to use the Workbook.
There are obviously other complicated & efficient ways to achieve this, but I prefer this one for Simplicity.
Looking at the linked to code source (what the actions are that follow that line) and the coding style of the author I think that Refresh_Serials is in fact a label. It should be Refresh_Serials:. The lack of the ":" means the compiler is expecting a sub/function of that name. As there are no GoTo statements these labels, there is another proper label further on,SerialVerified:, are unnecessary and confusing. They could simply be comments. Better still, go more Single Responsibility Principle and have functions/subs with names akin to those labels and shift the relevant re-factored code into them.

Excel cell locked when it is not locked

I have experienced the exact same problem as mentioned in this post.
Excel thinking cells are locked when they aren't
I tried to ask for clarification by responding / reviving the thread but it appears this is not the correct protocol. I only came across this post after numerous search string searches and this is the only relevant "hit" I could find.
My spreadsheet is similarly complex and has been running flawlessly through Excel 98, 2003 and 2010. When I use Excel 2013 (and I believe same problem happens with Excel 2016) I find cells which are unprotected (for data entry) become locked following use of a macro button, typically navigating to view a graph and then returning. Properties of the cell confirms the cell is not protected.
Reading the above post and the Microsoft links, I have only a shallow understanding of the problem i.e. SDI / Single Document Interface.
The above referenced post received a solution that was acknowledged as having resolved the issue. Perhaps I did not fully understand but no matter where I placed the code snipped it did not help me.
The code offered was:
Private Sub UserForm_Layout()
Static fSetModal As Boolean
If fSetModal = False Then
fSetModal = True
Me.Hide
Me.Show 1
End If
End Sub
There was another suggestion to try: MyForm.Show False I did not know where to pace this similarly to the main suggested code to resolve.
VBA did not like me using "Me" (Me.Hide / Me.Show 1). I tried place the code snippet in several places ... the main opening macro, the navigation macro to and from the sheet that is affected, changing "Me" to the sheet name all to no avail. It is a sheet but since the unique variables are gathered here, it may loosely be viewed as a "Form".
I used to be a competent amateur but never an expert. I am appealing for a kind person to help direct me to overcome this "Excel 2013" problem. I have tried and failed many times and spent many hours trying to fix only to find the problem is because I'm using Excel 2013. There are even now no issues using Excel 2010 which I am able to access via a friend.
I'm sorry my first post is so detailed and hopefully not over-complex. I only wanted to give the correct information about the problem.

Excel Workbook Open Event macro doesn't always run

I've got a Workbook_Open event macro (and it's in ThisWorkbook) that doesn't always run.
If Excel is closed and I double-click the .xls file from Windows Explorer, it does run.
If I launch Excel from Start/Programs (with no .xls file) and then open the file, it does run.
If I've already opened the file in Excel, but then close the file (leaving Excel open) and reopen it, then the macro does not run.
I've got security set to medium and I enable macros whenever it opens.
What do I need to do to get the macro to run whenever I open it, not just the first time for this Excel session?
(btw, this is Excel 2003)
I thought that this was the most cogent article on this problem (which is a long-standing never explained completely erratic bug that Excel exhibits). (dead link)
In short, in many cases it's a timing thing caused because the workbook is trying to calculate stuff when it opens and that gets in the way of the Workbook_Open event. The way to test on yours to see if that it for this situation, is to first rename any UDFs or Macros called by cells so that they won't get called and see if that changes anything.
I experienced the same problem.
I tested a workbook on my computer without any troubles. After destributing it to my customers I was told, that some combo-boxes stayed empty. These are usually filled from inside the workbook_open routine.
I tried different things to enable the workbook_open-Event - without success.
Finally, I found that disabling all userdefined Functions (UDF) lead to correct execution of workbook_open.
As my workbook is opened from another file, I will try to set calculation to manual first and then run the workbook_open manually. This may be done by defining it
public sub workbook_open
instead of
private sub workbook_open
Strange, that excel does not time this by itself...
A late answer (better than none).
I've had this problem now a few times (with Excel 2010).
The solution that has always worked (so far) was: remove conditional formatting, in particular if it contains UDF as conditions. As #LanceRoberts wrote in an above post, it's ultimately due to UDF calculations "overriding" the Open event, but I've found that those are particularly harmful if used in conditional formats.
I was experiencing almost identical behavior, and found that it is due to a bug that occurs if conditional formatting rules are erroring out. It turns out that if the conditional formatting rules are based on any setup by the macros, and that causes the conditional formatting to error, the Workbook_Open macro will not even attempt to run.
To test, make a copy of your file, and delete all conditional formatting from the workbook. Save and reopen. If it fixes your issue, then rework the conditional formatting rules to not depend on functions/values that will be broken before the Workbook_Open macro runs.
A few suggestions:
Try signing the workbook with a digital certificate. Add this certificate to the Trusted Certificates store then try again.
If this is machine-specific, try re-installing Office.
Make sure you have the latest service pack(s) applied.
I encountered the same problem, and I avoid it using the security settings.
I use the options settings then confidentiality center, then "params of confidentiality center" (sorry but its a translation of the french version :-p)
then "files approuved" or something like this.
And add the file containing the excel workbook in.
And its finnaly worked after that.
Looked everywhere and never find that solution.
Hope it'll help someone
This happens when a workbook is closed with an Application.EnableEvents set to false, and then you open another workbook within the same instance of excel opened. To avoid this, make sure that all of your processes that disable events, reenable them before terminating. Special attention to "End" commands, error handlers and "exit sub" sentences in the middle of your program.
What causes it is that your other archive, the one you openned first, have a Workbook_Open procedure; Excel doesn't excute it a second time.
This happened to me also and took me hours to figure out.
Turns out the TODAY() function in Excel was causing the problem.
Once deleted from my worksheet everything worked again. Very strange bug.
To add to the Arturo Llano post: The following code was used to monitor the Workbook_Open event and then run ProcessX whenever a workbook was opened.
ProcessX contained an End statement. The result was that it worked only the first time. The End wiped out AppX, so there was no further monitoring of events. Removing End fixed the problem. (Using End is bad practice anyway as it stops everything without any kind of cleanup or termination of other resources).
'Code in: Personal.xlsb ThisWorkbook
Public WithEvents AppX As Application
Private Sub Workbook_Open()
Set AppX = Application
End Sub
Private Sub AppX_WorkbookOpen(ByVal wb As Workbook)
'A 1-second delay to allow opening to complete before ProcessX starts.
Application.OnTime Now + TimeValue("00:00:01"), "ProcessX"
End Sub

Resources