How do I prevent unlocked cells from being selected/editable? (VBA) - 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.

Related

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!

Formula-based list for data validation

Please refer to the image attachment below. I want to ask for help regarding the semi-automation of the values D2:D6. Currently, they have the following formula:
=IF(C2="n/a",$D$12,IF(C2="","",IF(OR(TODAY()=C2,TODAY()>C2),$D$11,$D$10)))
Column C on the other hand has the following formula:
=IF(B2="n/a","N/A",IF(B2="","",$B$8-$B2))
Basically, what these formulas do is that after I input a value 'x' at column B, column C automatically indicates the date 'x' days before the start date on B8. Column D then indicates whether the deadline is due, not due, not applicable, or ready (D2:D6 is data validated with a list including D9:D13).
However, if a certain action is already completed, the "Ready" option is manually selected from the drop down list, which then erases the formula. What I wish to ask for help with is how do I return the erased formula in case the "Ready" option is just accidentally selected, or if my (not excel-capable) peers will need to reuse the form by just deleting items in column A and B.
What I tried doing was making the formula itself an option in the drop down list, but this doesn't adjust to the cell addresses for each item, and only shows one static value.
One option I thought of was to limit the data validation list with just the "Ready" option, so that the user can just delete the cell. A VBA code will then detect the blank cell and will return the formula it initially had. Unfortunately, I don't really know VBA or any programming. I can however understand and have managed to use and modify VBA codes posted in the internet according to my previous needs. I tried searching for a code, but to no avail.
If you could give me good references (not a whole book please) relative to this, I would really appreciate it. If you're feeling benevolent and help me with the coding, thanks! However, if there's a non-VBA solution, then I would really like to know more about this. Thanks all!
Sample list

extract data in exel sheet using macro

you most probably going to think "what an idiot" but remember i never done any type of coding before so this is all new to me,
My problem are that i'm working on a HUGE excel sheet with loads of data that is not needed. i need to sort the data into a few columns, i only need column "A,K,AN,AQ" but in column "AS" i only need certain values (yes,no,blank) i only want the yes and blank values. like i said never done any coding before but i know that you can use an macro to do it so please help, how do i go about this?
before trying to get into macros, try to use functions with if else statements. They are quite easy to handle. Like: If (yes) then put it into X. Later, you could select all needed. Also, check the, how the dollar sign is used
use this links to see, if it is something for you.
One quick and dirty way of getting this job done would be to:
Delete the columns you don't need.
Select all cells in the range you're interested in, click the Insert menu, and choose "Table". If your columns have titles, select the box for "My Table has Headers."
-This turns your data into an array so that Excel recognizes that each row is an entry (instead of thinking that the cells are unrelated).
Now you can use the filter icon in the column headers to select and display only the rows containing the values in column X that you're interested in.
Note that there are some limitations to what the table feature is good for, so, as always, whether this is a good solution for you depends on what you want to do with the data.

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.

Excel - How do I find all relevant rows by typing unique invoice# listed Col A

I have a Worksheet with 10 columns and data range from A1:J55. Col A has the invoice # and rest of the columns have other demographic data. Goal is to type the invoice number on a cell and display all the rows matching the invoice number from col A.
Besides auto filter function, the only thing comes to my mind is VBA. Please advice what is the best way to get the data. Thanks for your help in advance.
Alright, I'm pretty proud of this one. Again avoiding VBA, this one uses the volatile formula OFFSET to keep moving its VLOOKUP search down the table until it's found all matches. Just make sure you paste enough rows of the formula that if there are many matches, there's room for all of them to appear. If you put a border around your match area then it would be clear if you ever ran out of room and needed to copy down the formula some more.
Again, in the main section, it's just a single formula (using index):
=IFERROR(INDEX($A$1:$J$200,$M3,MATCH(N$2,$A$1:$J$1,0)),"")
This gets to be so simple because the hard work of the lookup is done by an initial column which looks up the next row that matches the invoice number. It has the formula:
=IFERROR(MATCH($L$2,OFFSET($A$1:$A$200,M2,0),0)+M2," ")
Here is the working example that goes with those formulas:
Let me know if you need any further description of how it works, but it mostly uses the same rules as above so that it's robust in copying and moving around.
I've uploaded the Excel file so you can play with it, but everything you need to reproduce this feature should be in this solution.
Google Docs - Click link and hit Ctrl+S to download and open in Excel.
A popular solution to this problem is a simple VLookup. Lookup the invoice the user types in on the table A1:J55, and then return an adjascent column's data.
Here's an example of it working:
The formula in the highlighted cell is:
=VLOOKUP($L3,$A:$J,MATCH(N$2,$1:$1,0),FALSE)
What's nice about this formula is you only need to type it once and then you can copy it across and it'll automatically pick out the correct column of the table (that's the match part). The rest is very simple:
The first part says lookup value $L3 (the invoice number typed in),
The second part says look it up in range $A:$J (which is where your table is located). I've shown how you can select the entire columns $A:$J so that you can add and remove data without worrying about adjustin the range in your lookups. (Excel takes care of optimizing the formula so that unused cells aren't checked)
The third part picks the column from which the resulting data will be drawn once a matching row is found.
The FALSE part is an indication that the invoice number must match exactly (no approximate matching allowed)
The $ signs ensure that fixed ranges like the location of your source table ($A:$J) and your lookup value ($L3) don't get automatically changed as you copy the formula across for multiple columns.
The formula is pretty easy to adapt if you want to move around your table and the area where you do your lookup. Here's an example:
Bonus
If you want to add a little spiff, you can add a dropdown to the Invoice # field so that the user gets auto-completion and the option to browse existing values like so: