SensualCarrots
New Member
- Joined
- Mar 21, 2015
- Messages
- 46
I have a workbook that is incredibly locked down, and I'm trying to find a way to accomplish something. My current code is...
This is a nested If statement in a public module that is triggered by the "Private Sub Worksheet_Change(ByVal Target As Range)" sub on Sheet 3.
Basically, Range D7:D65 is a user field where we enter part numbers, and an Index(Match) formula in the cell to the right compares the value against a database on a hidden sheet, and if found populates the cost. (Currently in the process of doing this using a dynamic array, but that will take me a while as I'm just learning how to build arrays) If the value does not exist in the database, then the cell offset 2 columns to the right is unlocked for user editing to provide the cost of the item. However, if the cell in D7:D65 is blank or contains a value within the database, the offset(0,2) cell in F stays locked so the user cannot override the cost of the item in the system. So when I delete the value in D, the adjacent cell in E locks. This all works perfectly, except..... If I try to delete or change multiple cells at a time, it breaks the code. I tried this without the extra If statement to check if the selection.count is 1 first, but that resulted in an error. I came here after implementing that If statement as it doesn't work as I'd like it to. I understand why, but can't think of a fix. I found a fix using a 'For Each' statement, but there are over 76,000 values in the database, and it takes too long to have to go through and do this every time a value in D7:D65 is changed, as each cell is processed individually. Is there a better way, or is an array my answer?
Code:
'Cost Override Values for Conditional Formatting
If Sheet4.Range("AT1") = True Then 'Sheet4 - AT1 Tracks whether or not the workbook and all worksheets are locked or not (this does not apply to advanced users)
If Not Intersect(Target, Sheet3.Range("D7:D65")) Is Nothing Then 'User field to enter part numbers
If Target.Count = 1 Then 'If only 1 cell selected
If Target.Offset(0, 1) = "Not Found" Then 'Index(Match) Formula in this cell to populate the cost of the item from the database. If not found, the text "Not Found" is entered here
If Target.Offset(0, 2).Locked = False Then Else Target.Offset(0, 2).Locked = False 'Conditional formatting looks to see if the cell to the left = "Not Found". If so, it colors the cell so the user knows they can enter data in it
Sheet4.Range("AU" & Target.Row) = False 'Tracks that the cell is unlocked
Else
If Target.Offset(0, 2).Locked = True Then Else Target.Offset(0, 2).Locked = True 'If the cell to the left does not = "Not Found" then lock the cell
Target.Offset(0, 2) = "" 'Then wipe its contents
Sheet4.Range("AU" & Target.Row) = True ''Tracks that the cell is locked
End If
Else
MsgBox "Please Only Change 1 Cell at a Time", vbOKOnly + vbExclamation, "Application Warning" 'I get the warning if I change more than one cell, but it still allows the user to do it, so really doesn't do anything
End
End If
End If
End If
This is a nested If statement in a public module that is triggered by the "Private Sub Worksheet_Change(ByVal Target As Range)" sub on Sheet 3.
Basically, Range D7:D65 is a user field where we enter part numbers, and an Index(Match) formula in the cell to the right compares the value against a database on a hidden sheet, and if found populates the cost. (Currently in the process of doing this using a dynamic array, but that will take me a while as I'm just learning how to build arrays) If the value does not exist in the database, then the cell offset 2 columns to the right is unlocked for user editing to provide the cost of the item. However, if the cell in D7:D65 is blank or contains a value within the database, the offset(0,2) cell in F stays locked so the user cannot override the cost of the item in the system. So when I delete the value in D, the adjacent cell in E locks. This all works perfectly, except..... If I try to delete or change multiple cells at a time, it breaks the code. I tried this without the extra If statement to check if the selection.count is 1 first, but that resulted in an error. I came here after implementing that If statement as it doesn't work as I'd like it to. I understand why, but can't think of a fix. I found a fix using a 'For Each' statement, but there are over 76,000 values in the database, and it takes too long to have to go through and do this every time a value in D7:D65 is changed, as each cell is processed individually. Is there a better way, or is an array my answer?