Sorting on a protected Excel sheet - vba

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.

Related

Is there a way to activate Autofilter only on specific columns in VBA?

I have a macro that creates a separate workbook from a larger document. The number of columns vary on how much data is entered. I would like to add an autofilter to only the currently used columns but every time I add an autofilter I have to put in a range and the range varies. Is there a way to write in the code to only add the filter arrow on the applicable columns and not all of them that could possibly be shown? We are sending the document out to a customer and have to manually unfilter the blank boxes which we would prefer not to do.
Thanks!
Turn your data into an Excel Table using the Ctrl + T keyboard shortcut. These have the autofilter built in, and it automatically gets applied to any new columns in the Table, because Tables expand automatically to accommodate any new data.
Furthermore, because Tables are basically Named Ranges that Excel maintains on your behalf, it is very easy to identify the ranges concerned in VBA, as per my answer at VBA coding to identify and clear data in a specific table

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.

Parameter to allow users to select locked cells?

What's the parameter to allow users to select locked cells? When you manually protect a worksheet there is an option for this, but I can't find it for VBA.
Sorry if this question has been asked but I can't find it with so many overlapping words with other results.
If you mean you that you want to keep certain cells unlocked when the worksheet is protected (so that users may select, edit, etc.) you can use:
Range("A1").Locked = False
Where A1 can be replaced with whichever range of cells you want to leave unlocked when the sheet is protected.
Alternatively, you may be able to accomplish what you need by using:
Worksheets("Sheet1").EnableSelection = xlNoRestrictions
And changing Sheet1 to whichever worksheet you want users to be able to select on.

Unable to move / delete rows in shared workbook - Not enough resources

this one's a bit of a painful one so thank you for your help and patience with me.
We have an Excel spreadsheet that we use as a master file for our website products. As such there are quite a few sheets and quite a few products on each running along side some macros to provide some extra functionality (turning entered data into HTML for product page, etc).
My issue is that one of our most used spreadsheets has become a trouble in that it has some phantom formatting all the way down to the millionth-and-something row and all the way across, causing the last cell to be the very last cell possible.
The issue that has finally popped up as a result is that we can no longer move rows in, out or around the sheet (a required functionality) as it results in an 'out of resources error'.
I've tried:
Highlight all rows below used range to right-click> delete - Results in runtime error (from macro)
Highlighting large chunks of rows and using Clear All - Resulted in the 38MB file bloating to 380MB
Deleting a chunk of rows at a time - Maxed out at 1,000 before it caused Excel to crash
Moving to new spreadsheet - Broke all our macros (which I did not write and am not proficient enough to fix on a new sheet)
Disabling macros and trying the above options, only marginally more efficient but still out of resources
I'm at my wits end on this one and, while we can continue with most day-to-day functions, we will soon be completely unable to use this particular sheet as we need it at all.
I'm wondering if there might be a way to run a VBA script to remove these rows, potentially one by one? I've tried running a short script that went something like rows[960,1000000].Delete (forgive my terrible VBA markup), but this also resulted in not enough resources errors.
I'm wondering if there's anything like:
row = 960;
while(row<=1048576){row.Delete};
Continuing, the runtime error debug points me to the below if statement within the macro:
If Target.Count > 1 Then Exit Sub
Where Target is the variable passed to the sub.
Which strikes me as very odd because my (limited) understanding of VBA and IF's in general simply recognizes that 'if my selection is larger than 1 (row?), do not run this code..
Thanks again in advance.
Use this method only if you don't have any links into or out of the sheet that will get broken. Also might have Sql connections that might get broken. Might need to disable macros. There are many possible problems with this approach. Use at your own risk.
Note the exact "Name" and "(Name)" of the sheet; Look in the VBA code window at the properties for the sheet. "Name" is the name displayed on the worksheet tab. "(Name)" is the code name visible only in the properties window.
Make a list of range names on the sheet.
Copy the data to a new sheet.
Copy any macros to the new sheet.
Delete the old sheet.
Rename the "Name" and "(Name)" of the new sheet the same as the old one.
Recreate range names.
A better method if you don't have too many formats:
Disable macros and set calculation to manual. This avoids recalculating while doing your delete operation.
Select entire sheet and clear formats.
Delete all rows below your data.
Redo your formatting. Select entire column (not just used area) to apply format if applicable.
It is important to remove formatting on the entire sheet from A1 to the end. Otherwise you'll get the bloat you mentioned. Just that step may solve your problem. If not then proceed with removing all the rows below the data. This should not cause file size bloat.

Permissions in Excel

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.