Why does VBA seem to have a memory?

XrayLemi

Board Regular
Joined
Aug 1, 2018
Messages
153
Office Version
  1. 365
Platform
  1. Windows
I have this code in a workbook.
VBA Code:
Dim cr As Range
     Set cl = Range("H6:H5000")
     Set cl = Intersect(Target, cl)
     For Each cl In Target
     Select Case True
     Case 8 = cl.Column 'H
     If cl.Value <> "" Then
        Check = MsgBox("Is this entry correct?" & vbCrLf & "This cell cannot be edited after entering a value.", vbYesNo + vbQuestion, "Cell Lock Notification")
            If Check = vbYes Then
            Target.Rows.EntireRow.Locked = True
            Else
            Cells(cr.Row, "H").Value = ""
            End If
          End If
      Case Else
    End Select
   Next cl
  Application.EnableEvents = True
End Sub
Everything works as it is coded. However, If I change the range to ("L6:L5000") and anything associated with the range, the message box STILL appears when you enter a value in column H. It should appear after an entry in column L. I have even copied the code to word, changed the code in Word, erased the old code, made a copy of the sheet, re-pasted the new code, and it still refers back to the old range!
Why? What am I missing?
Thanks in advance,
Jim
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
For a start these two lines are redundant (ie they don't do anything)
VBA Code:
    Set cl = Range("H6:H5000")
     Set cl = Intersect(Target, cl)
Can you post the entire code.
 
Upvote 0
Good morning Fluff. Here it is.
VBA Code:
Private Sub CommandButton1_Click()
    UpdateDataFromMasterFile
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim r As Range, c As Range
  Set r = Range("G6:G5000")
  Set r = Intersect(Target, r)
  If Not r Is Nothing Then
  Application.EnableEvents = False
  For Each c In r
    Select Case True
      Case 7 = c.Column 'G
        If c.Value = "" Then
          Cells(c.Row, "H").Value = ""
          Cells(c.Row, "H").Locked = True
          Else
          Cells(c.Row, "H").Locked = False
        End If
      Case Else
    End Select
  Next c
  End If
   If Target.Cells.Count > 4 Then Exit Sub
  If Not Intersect(Target, Range("B6:B5000")) Is Nothing Then
    With Target(1, 4)
     .Value = Date
     .EntireColumn.AutoFit
    End With
    End If
     Dim p As Range
     Set p = Range("L6:L5000")
     Set p = Intersect(Target, p)
     For Each p In Target
     Select Case True
     Case 8 = p.Column 'L
     If p.Value <> "" Then
        Check = MsgBox("Is this entry correct?" & vbCrLf & "This cell cannot be edited after entering a value.", vbYesNo + vbQuestion, "Cell Lock Notification")
            If Check = vbYes Then
            Target.Rows.EntireRow.Locked = True
            Else
            Cells(p.Row, "L").Value = ""
            End If
          End If
      Case Else
    End Select
   Next p
  Application.EnableEvents = True
End Sub
 
Upvote 0
Try it like
VBA Code:
   Dim p As Range
   Set p = Range("L6:L5000")
   Set p = Intersect(Target, p)
   For Each c In p
      Select Case True
         Case 12 = p.Column 'L
            If c.Value <> "" Then
               Check = MsgBox("Is this entry correct?" & vbCrLf & "This cell cannot be edited after entering a value.", vbYesNo + vbQuestion, "Cell Lock Notification")
               If Check = vbYes Then
                  c.EntireRow.Locked = True
               Else
                  Cells(c.Row, "L").Value = ""
            End If
         End If
         Case Else
      End Select
   Next c
 
Upvote 0
Fluff,
When I copy the code I get a runtime 424 object required error. When I hit debug, the line of code: For Each c In p highlights in yellow.
 
Upvote 0
Try adding this line
Rich (BB code):
   Set p = Intersect(Target, p)
   If p Is Nothing Then Exit Sub
   For Each c In p
 
Upvote 0
Okay, now it compiles with no errors but.... The message box does not come up and the row does not lock.
 
Upvote 0
How are you changing the values in col L?
 
Upvote 0
It is a simple name entry. No pull downs, no copy / paste.
 
Upvote 0
So they are manually entered, one cell at a time?
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top