So someone recently helped me with implementing a way to manage data updates using a sheet change event within a lookuplist sheet and it works great...however only if the user updates values on the sheet directly. The goal is this list will be hidden and controlled using only forms allowing the user to work within the forms regardless of which sheet is selected. Many thanks to mart37 for the code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewValue As Variant
Dim OldValue As Variant
Dim SearchRange As Range
Dim Found As Variant
Dim FirstAddress As Variant
Dim CountReplaces As Integer
If Intersect(Target, ActiveSheet.Columns(7)) Is Nothing Then Exit Sub
If IsEmpty(Target) Then
MsgBox "Cell is empty!" '\\I don't know if I really need this
Exit Sub
End If
On Error GoTo Err:
Application.EnableEvents = False
NewValue = Target
Application.Undo
OldValue = Target
Target = NewValue
Set SearchRange = Sheets(7).Range(Sheets(7).Cells(2, 2), Sheets(7).Cells(Rows.Count, 1).End(xlUp).Address)
With SearchRange
Set Found = .Find(OldValue, LookIn:=xlValues)
If Not Found Is Nothing Then
FirstAddress = Found.Address
CountReplaces = 0
Do
Sheets(7).Range(Found.Address) = NewValue
CountReplaces = CountReplaces + 1
Set Found = .FindNext(Found)
If Found Is Nothing Then GoTo DoneFinding
Loop While Found.Address <> FirstAddress
DoneFinding:
If CountReplaces > 0 Then MsgBox ("This group was replaced" & CountReplaces & " times within the distribution list.")
End If
End With
Err:
Application.EnableEvents = True
End Sub
When a value is updated (or added) when not on the lookup sheet, the error occurs on:
If Intersect(Target, Sheets(5).Columns(7)) Is Nothing Then Exit Sub
I've changed ActiveSheet to Sheets(5) - but I'm not sure if I'm referencing the sheets correctly. Regardless, it doesn't seem to make a difference.
Bookmarks