Parameter to allow users to select locked cells? - vba

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.

Related

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.

Display change in value of a cell in adjacent cell using excel VBA

I have a excel sheet that display's price on certain items in a column by looking up amazon API using excel vba. The price of may change overtime. So I am trying to display the difference in prices each time i run my macro, in a cell adjacent to the cell that displays price.
But I am not sure how to achieve this. Can any body guide me on how to achieve this?
This is just a sample, it must be adapted to your schema and data layout. Say the prices are stored in column A from A1 to A100. Say you already have a macro called RefreshData() that updates column A. In B1 enter:
=C1-A1
and copy down. This macro store the current values in column C before refreshing the data:
Sub DoUpdate()
Range("A1:A100").Copy Range("C1")
Call RefreshData
End Sub
Column B will display the price difference.
Something like this?
Let's say your data are in a range A2:A10
Dim rng as Range
Set rng = Range("A2:A10")
rng.Offset(0,1).Value = rng.Value
Run this before you run your original macro to store the values in an adjacent column before the values change. You may need to make the range dynamic, depending on your needs.
Without seeing you code, I cannot give a detailed answer. However, I ran across a similar problem once, not using Amazon API though, but a sharepoint connection.
If the amazon api is somewhat similar to the sharepoint stuff, I guess it refreshes cells when you click "update", or run the update sub. In that case you will have to either create an array to store the old prices in vba (very slow process), and then write them to your table, or create a separate tab where you store the item-lastPrice combination.
I ended up storing not only current price but all prices and the date/time of the price, to be able to see change over time.
For the copying of data itself using VBA, either of the above methods should work. In my initial code I used vba loops :-p, but copying using excel functionality is much faster.

How do I select specific cells in VBA?

Basically I need to select specific cells on an excel sheet in vba. For example the code should be able to select a5, a10, a15. I do not want the cells in between them, just the ones I listed. Is there a specific function that will do this? It seems like .Range could only take start cell and ending cell.
You'd use this: Range("A5,A10,A15").Select
To add additional cells, just use more commas.
Alternately, you can utilize the Union method to join multiple range objects into a single Range object.
Note that it is generally not a good idea to select cells in VBA as nearly everything can be done without selection. This is a frequent mistake made by people new to VBA due to generated macros (Record Macro) recreating what you do rather than the result you want.

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.

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.