Permissions in Excel - vba

I have a sheet in Excel that has permissions set up to "Allow Users to Edit Ranges".
These different ranges consist of columns that users with different permissions can edit.
Is there any way to exclude cells/rows from these ranges?
For instance, I want to say I want the range of everything in column A except when cells in in column G of that row have nothing in them.
Alternatively, is there any way to use Macros to print out these complicated ranges?

You can select the cells with vba or manually and set the locked property to false before locking the sheet. This will allow the user to edit those range/cells.
You can use conditional formatting to identify these locked cells.
Select your range (ctrl-A for all the cells).
Conditional formatting
formula is: =CELL("protect",A1)=0

"Allow Users to Edit Ranges" only allows us to specify the range(s), not to apply a rule that would allow, or disallow, editing.
You could use Data Validation to produce an error message. For example, creating a Custom validation rule of
=K2<>""
in a particular cell would mean that an error message will be produced if they attempt to put a value into this other cell if K2 is empty. You need to uncheck the box which says Ignore Blank.
This is not foolproof though, as a Copy/Paste would wipe-out the rule.
It can be done with VBA, but even then it could be circumvented with Copy/Paste. However, the (for example) BeforeSave event could be used to check the content of these cells and produce a message and prevent the save.
Disclaimer: I don't know whether VBA works with a file that uses the "Allow Users to Edit Ranges" feature.

Related

A: Hide rows in Excel VBA when target cell contents are deleted

I'm working on a VBA in excel that hides or un-hides rows on other worksheets based on the content of target cell specific drop-down lists. The drop down lists are meant to be dynamic, and users will occasionally change the responses. Everything works well when responses are changed except when a user might delete previously entered content within a target cell with the delete button. If this occurs, the hide/un-hide feature on the other sheets doesn't react.
Is there a line of code I can use to make True/False a hidden row command based on a user simply deleting content in a target cell (or multiple target cells if the user was to try to clear the whole front-end form by deleted content across all the drop down lists in all targeted cells?)?
Thanks in advance for the help!

Excel Named Range Formula - Not Automatic Updating

I have a table in Excel that has column heading names (e.g. data_type1, data_type2, etc.). The data in this table changes based on parameters entered on another sheet, and they are pulled to charts which update dynamically.
As a convenience to a user who might be using this sheet I have added a 'user specified function' (non-vba) which also plots to one of the charts. By user specified function I mean I have three cells with dropdown lists. Two correspond to the table headings and one has a short list of operations that can be applied between the two selected data types (e.g. a user might select 'dataype1', '+', 'datatype2' which would produce a sum of the two in the final column of my table).
The user specified function is achieved by defining a named 'range/function' to match the drop downs with their respective column headers and then calls evaluate. See below:
=EVALUATE("="&ADDRESS(ROW('Raw Data'!XFD5),MATCH(user_in1,'Raw Data'!$A$4:$AF$4,0)) & user_operation & ADDRESS(ROW('Raw Data'!XFD5),MATCH(user_in2,'Raw Data'!$A$4:$AF$4,0)))
I name this 'user1_result' and then enter =user1_result in the final column of my table. This approach is nice because it calculates much faster than doing the same thing through building a UDF in VBA and then applying that UDF to every cell in a fairly long column.
Now here is my hangup, this works fine initially, but if the user makes a parameter change that affects one or both of the selected datatypes, the user specified column does not recalculate on-the-fly with the updated data. If the user re-toggles any of the dropdowns the data does recalculate. I am speculating this is from one of two things:
1) Excel does not recognize that a precedent of 'user1_result' has changed, and so for efficiency sake doesn't bother to recompute the column;
2) The 'Evaluate' function used in the named definition of 'user1_result' is not checked for updating, because it's not a normal function (doesn't show up through intellisense if you try to just add that to a cell).
So I am looking for some either confirmation or refutation of these speculations. In the case of confirmation I am hoping to get some advice on how to force the user specified column to update if its precedents change.
One solution is to have VBA do this checking for me and force the computation, but I would like to leave that as a last resort. So, non-VBA solutions preferred.
For posterity I'll answer the above question based on Mat'sMug's feedback:
Regarding the cause of the problem:
The reason the user specified column does not update is because the 'Evaluate' portion of the 'user1_result' named formula is intended to be used at the application level and not as a worksheet function. Because of this, Excel doesn't bother checking to see if its precedents change and ignores it for recalculation.
The problem's solution:
It was suggested to use VBA to watch for worksheet_change events, however, my problem requires that I do NOT use VBA. So, an alternative workaround that forces Excel to check precedents and recalculate the user specified function uses two steps. This functions as a pseudo worksheet_change stand-in.
First, I use a helper cell that performs a countif with an arbitrary counting condition. I don't need it to change, I just need it to share precedents with the inputs of 'user1_result'. So I have it count the number of cells in the first row of data that are larger than some constant:
=COUNTIF(A5:AK5,">100000")
The result of this computation doesn't matter, but in my case my data have small values and so this returns 0 always.
Second, I use a condition for the computation in the user specified column (the last column in my data table).
=IF($AO$1=$AO$1,user1_result)
Now, anytime my data table updates, the final column using the named function recomputes. Simple, and if using macros is not viable (for example due to a client/user's security concerns), this can sort of substitute for a worksheet change event.
I hope somebody out there gets use from this!

Sorting on a protected Excel sheet

i have an excel related problem. I already searched older questions but neither of them have a good anwser for the problem.
I want to be able to sort, filter and use pivot tables on a protected sheet. I researched so far that one can not sort on a protected sheet. But is there a way to unlock the sheet for sorting and then lock it again after successful sorting?
thx in adavance
In general, you can choose things to allow when you protect a sheet, such as filtering (when you click on "Protect Sheet", there is a list titled "Allow all users of this worksheet to:").
With sorting, it really depends on whether the cells in the table are locked or not. If not, it works just by allowing it in the protection menu. If some are locked, there is a workaround with
Review (Ribbon) > Allow users to edit ranges
whilst disallowing selection of locked cells. Here you basically add the locked ranges of your table to the "editable" ranges, but at the same time you disallow users to click on them, so that they stay in fact uneditable. But this method does come with its drawbacks, I'm sure you can find more details on google.
About pivot tables, try checking the option "Use pivot table reports" in the list when locking the worksheet.
You'll need to right click on the filter cells > Format cells > Protection tab > untick 'Locked'. Then protect the worksheet tick the options to 'Sort', 'Use Autofilter', 'Use PivotTable reports'. This will allow the user to sort, filter etc but will also allow them to edit the cells directly i.e. not just using the sort button. You could untick the 'Select locked cells' option when you protect the worksheet to prevent this direct editing.

How do I prevent unlocked cells from being selected/editable? (VBA)

I have a very specific question that I am looking for help in answering. I have been researching for hours and I feel like I am not able to find what I am looking for. Below is a quick overview of the criteria that my document must follow:
I am using Excel 2013
I will just be using rows for data input (instead of an excel object
table).
The very top/first row will act as my "column header".
This top row will have AutoFilter enabled.
THE DOCUMENT MUST BE PROTECTED (a must-have)!
I will be using VBA code
Now, the final issue I am having with finishing this document are the last two criteria points that I must have:
The first/top row (column headers) must NOT BE EDITABLE.
Each column must be able to SORT AND FILTER.
Now, in a perfect world, I would just "Lock Cells" for the entire first row that acts as my column headers and when I protect the worksheet I would make sure to check the "Sort" and "Use AutoFilter" boxes.
However, this option does not work because there seems to be an issue when I try to sort the data. If I just filter the data there is no problem, but when I try to sort a column in ascending/descending order I will get an error informing me that I can't sort locked cells while in Protected mode. This is because when excel uses the Sort function, it counts the header as part of the data that is being sorted (I found this out through my research) even though I really just want the data below it to be sorted.
I have been trying to brainstorm on how to get past this issue as well as researching different methods, and I am having trouble coming to a final conclusion. However I have narrowed it down to 2 possible solutions:
I want to be able to keep the cells in the first row officially unlocked to allow the AutoFilter's sort command to work as intended, but make it "behave" like the cells are locked when a user tries to make changes to it (AKA, make the entire row un-editable or un-selectable).
The other option would be to keep the first row locked but somehow have an event in VBA that can tell when a user tries to "Sort" the column, which will then temporarily unprotect the worksheet, follow through with the intended sort command, then protect the worksheet again (apparently though, upon my research there is no such event that can trigger off the AutoFilter's sort command alone).
These 2 solutions are the most logical I can think of based off my research, but if someone out there is an Excel genius and knows another way I am open to suggestions.
Thanks in advance for your help/suggestions,
Travo
Consider using two header rows. The top row would be protected and the second row would facilitate filtering and sorting of the data in rows 3 and below.

Does having a workbook that contains links to others affect the VBA code within it?

I have a workbook that uses values in the cells to generate tables used in Prophet. If instead of having values in cells, I have links to other workbooks, will this affect the VBA code in anyway? Are there any concerns that I need that I need to be aware of?
There will not be an effect to your macro.
Excel stores the value, as well as the link. So even if the link is not refreshed or the 'linked to' workbook is missing, there is still a value to return.
This snippet of code will return the cell value regardless of the link status.
A1 = Range("A1").Value
The biggest danger is users not refreshing the link, in which case you could be using old values. It is also possible for your users to break the link which converts the link from a formula to a value.