WildBurrow
New Member
- Joined
- Apr 5, 2021
- Messages
- 41
- Office Version
- 365
- Platform
- Windows
I've noticed that my code will run correctly with the first select; however, if the User goes back and changes the cell value the code will not run. Basically, they may "toggle" back and forth with their answers. When I place the Application.EnableEvents=True in the immediate window, things will re-trigger. I then found some code to handle errors ('Worksheet change events stop working after Debug? by dmt32) and tacked on an 'Error' message box. To my horror, the message box trigger for every cell noted in my code!!!! Any advice on what is going on and how to fix it?
So this my code:
So this my code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo myerror
'snippet of code below:
Set ws2 = Worksheets("Tables")
'If ID Type equals API format ID number:
If Not Application.Intersect(Target, Range("G35")) Is Nothing And Range("B35").Value = "API" And ws2.Range("V3").Value = "Format" Then
Target.NumberFormat = "00-000-0-0000-0000"
If IsNumeric(Target.Value) = False Or Len(Target.Value) <> 14 Then
msgbox "Numeric values only - 14 digits required."
Application.EnableEvents = False
Target.Value = ""
Target.Activate
Application.EnableEvents = True
End If
End If
Select Case Target.Address
Case "$I$5" 'Control access to Dashboard command buttons based upon 1509 Status
If Target.Address <> "$I$5" Then Exit Sub
If Target.Value = "1509 Related" And Range("I8").Value = "Stray Gas" Then
Worksheets("Dashboard").CommandButton10.Enabled = True
Worksheets("Dashboard").CommandButton2.Enabled = False
Worksheets("Dashboard").CommandButton3.Enabled = False
Worksheets("Dashboard").CommandButton4.Enabled = False
End If
'Remediation#1 Completed interior color change if not N/A
Case "$O$164"
If Target.Address <> "$O$164" Then Exit Sub
If Target.Value <> "" Then
Range("S164").Interior.Color = RGB(255, 255, 0)
End If
Case "$S$164"
If Target.Address <> "$S$164" Then Exit Sub
If Target.Value = "No" Then
Target.Interior.Color = RGB(255, 255, 255)
Range("W164").Interior.Color = RGB(231, 230, 230)
Range("AA164").Interior.Color = RGB(231, 230, 230)
ElseIf Target.Value = "Yes" Then
Target.Interior.Color = RGB(255, 255, 255)
Range("W164").Interior.Color = RGB(255, 255, 0)
Range("AA164").Interior.Color = RGB(255, 255, 0)
End If
Case "$W$164"
If Target.Address <> "$W$164" Then Exit Sub
If Target.Value <> "" Then
Target.Interior.Color = RGB(255, 255, 255)
End If
Case "$AA$164"
If Target.Address <> "$AA$164" Then Exit Sub
If Target.Value <> "" Then
Target.Interior.Color = RGB(255, 255, 255)
End If
If Target.Value = "Yes" Then 'Store temporary change value to audit attachment requirements
Range("I272").Value = True
ElseIf Target.Value <> "Yes" Then
Range("I272").Value = False
End If
'a ton of code here
Case Else
Exit Sub
End Select
myerror:
Application.EnableEvents = True
msgbox "Error"
End Sub