I am a real newbie when it comes to VBA and I am limited to copying and pasting codes I search for online - in addition to finding help here when needed **thanks to the super moderators who have already helped** so here is my current issue:
I have a code that changes the colour of a cell (my conditional formats are maxed out)
Although I have [Application.Volatile] in the code, it does not actually change the colour of the cell as I go along. Perhaps I have inserted it incorrectly? So, I added a button and assigned the above macro to it - works great when the workbook is unprotected, but I get a runtime error '1004' when the workbook is locked. I have searched for and tried a variety of codes to get around this such as:Sub ColorValues() Application.Volatile Dim Values As Range Dim cell As Variant Set Values = Range("A3:F28") For Each cell In Values If cell.Value = "OT" Or cell.Value = "A/P/C" _ Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then cell.Font.ColorIndex = 10 End If Next cell End Sub
Sub ProtectUIOnly() ActiveSheet.Unprotect Password:="mypassword" ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True End Sub
But since I don't really know what I am doing, it doesn't work (runtime error '1004'). I can only assume that I have inserted it incorrectly into the existing code or in the wrong place etc. (I currently have this code as its own module and I get the runtime error)
The solution I need is this: The workbook needs to be protected so the users do not accidentally change anything, but I might not need the 'unlock' macro if I can get the 'Application.Volatile' part to work - changing the colour as I go (this is a drop-down by the way). Any help, explanations etc. would be much appreciated. Workbook attached...
thanks in advance
Last edited by Greed; 08-17-2011 at 03:47 PM. Reason: Solved
Apologies - I thought by typing them in, it would have the same effect - apparently not, please excuse my ignorance.
Greed:
(1) You can type your code tags. I think you initially closed your code tag with [\code] instead of [/code]. (Note the orientation of the slash.
(2) In order to run code on a protected sheet, you have to think in turns of chronology. So, you would have to first unprotect the sheet, then run the code, then reprotect it. Without doing any troubleshooting, your code should read like the following:
Let me know if this works and if there is anything else with which I can help.Sub ProtectUIOnly() ActiveSheet.Unprotect Password:="mypassword" Dim Values As Range Dim cell As Variant Set Values = Range("A3:F28") For Each cell In Values If cell.Value = "OT" Or cell.Value = "A/P/C" _ Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then cell.Font.ColorIndex = 10 End If Next cell ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True End Sub
Yes! That's just the thing, and I thank you for the explanation - I did have to alter the button to make it work as I was initially getting another error, but it works now so that's great.
I feel like an idiot not noticing the / vs \ in my code tags though....
Can anyone explain why the 'Application.Volatile' portion of my code isn't working for me? Is it because I have made an error, or because it is not effective on cells that are drop-downs perhaps?
Thanks again!
Application.Volatile, to my understanding, applies more to user defined functions, essentially forcing a recaculation whenever a change is made to a spreadsheet. It can also slow down the functionality of the spreadsheet.
In your case, I think you should do some research on Worksheet_Change events. These are placed into the worksheet code instead of a standard module, and can trigger a macro based on a change. For example, if you added this code directly to the worksheet module, it would run your macro ANYTIME a change is made to cell A1 in the corresponding sheet:
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then ActiveSheet.Unprotect Password:="mypassword" Dim Values As Range Dim cell As Variant Set Values = Range("A3:F28") For Each cell In Values If cell.Value = "OT" Or cell.Value = "A/P/C" _ Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then cell.Font.ColorIndex = 10 End If Next cell ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True End If End Sub
Thanks, the explanation helps. I will continue to study up on my Excel skills - sometimes I wish I didn't have a job so I would have more time to learnI find it truely fascinating...
A1 is a locked cell that never changes. With the code you provided, am I able to replace $A$1 with a range instead?
Greed,
The TARGET refers to the cell that is being changed, so it can never truly be a range of cells. There are ways around this. For example, instead of returning the address of the target, you can search the column of the target. To trigger the macro anytime a change is made to any cell in column B, the code would read:
If you want to run a macro whenever there is a change to a number of cells, you can use UNION. The following code will trigger your macro whenever you make a change to any cells in range A4:F25:Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 2 Then ActiveSheet.Unprotect Password:="mypassword" Dim Values As Range Dim cell As Variant Set Values = Range("A3:F28") For Each cell In Values If cell.Value = "OT" Or cell.Value = "A/P/C" _ Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then cell.Font.ColorIndex = 10 End If Next cell ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True End If End Sub
Private Sub Worksheet_Change(ByVal Target As Range) If Not Union(Target, Range("A4:F25")) Is Nothing Then ActiveSheet.Unprotect Password:="mypassword" Dim Values As Range Dim cell As Variant Set Values = Range("A3:F28") For Each cell In Values If cell.Value = "OT" Or cell.Value = "A/P/C" _ Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then cell.Font.ColorIndex = 10 End If Next cell ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True End If End Sub
Thanks - unfortunately the 2nd code (referring to UNION) does not function as expected...the cells do not change colour once the corresponding codes (A/C, OT etc.) are selected - again, not sure if this is because the text is selected from a drop-down list? No worries though, the insertion of the unprotect/protect code is enabling the macro button so I'm happy!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks