Hi folks.
I have a spreadsheet with VBA code that has allot of protections on it. Basically the only row on the spreadsheet that is "unlocked" for data entry is the row being used. All the rows before and after are locked. The code I wrote worked fine for days in the test files. When I added the code to the user files, I get a Run-time error '1004': Select method of range class failed.
After some research it appears that using .select may actually cause this after repeated use. This is the line of code the error appears on.
This line of code was added because when you enter YES in a MsgBox, The row you are currently working on locks and the next row unlocks. Before I added the above line of code, sometimes the row would lock, sometimes not. Sometimes other weird things would happen. After adding that line of code, all worked well. Now suddenly out of nowhere I get the error. the attached images show what I mean.
Here is the entire code.
All I need is to make sure when the user enters YES, the cell in column B in the next row is the next cell ready for data input. Any ideas?
Thank you,
Jim Lemieux
I have a spreadsheet with VBA code that has allot of protections on it. Basically the only row on the spreadsheet that is "unlocked" for data entry is the row being used. All the rows before and after are locked. The code I wrote worked fine for days in the test files. When I added the code to the user files, I get a Run-time error '1004': Select method of range class failed.
After some research it appears that using .select may actually cause this after repeated use. This is the line of code the error appears on.
VBA Code:
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
This line of code was added because when you enter YES in a MsgBox, The row you are currently working on locks and the next row unlocks. Before I added the above line of code, sometimes the row would lock, sometimes not. Sometimes other weird things would happen. After adding that line of code, all worked well. Now suddenly out of nowhere I get the error. the attached images show what I mean.
Here is the entire code.
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 = Union(Range("J6:J5000"), 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 10 = c.Column 'J
If c.Value = "" Then
Cells(c.Row, "L").Value = ""
Cells(c.Row, "L").Locked = True
Else
Cells(c.Row, "L").Locked = False
End If
Case 7 = c.Column 'G
If c.Value = "Not Listed" Then
Cells(c.Row, "H").Locked = False
Else
Cells(c.Row, "H").Locked = True
Cells(c.Row, "H").Value = ""
End If
Case Else
End Select
Next c
End If
If Target.Cells.Count > 3 Then Exit Sub
If Not Intersect(Target, Range("C6:C5000")) Is Nothing Then
With Target(1, 3)
.Value = Date
.EntireColumn.AutoFit
End With
End If
Dim p As Range, z As Range
Set p = Range("M6:M5000")
Set p = Intersect(Target, p)
If Not p Is Nothing Then
Application.EnableEvents = False
For Each z In p
Select Case True
Case 13 = z.Column 'M
If z.Value <> "" Then
Check = MsgBox("Are your entries correct?" & vbCrLf & "After entering yes, These values CANNOT be changed.", vbYesNo + vbQuestion, "Cell Lock Notification")
If Check = vbYes Then
Cells(z.Row + 1, "B").Locked = False
Cells(z.Row + 1, "C").Locked = False
Cells(z.Row + 1, "D").Locked = False
Cells(z.Row + 1, "F").Locked = False
Cells(z.Row + 1, "G").Locked = False
Cells(z.Row + 1, "I").Locked = False
Cells(z.Row + 1, "J").Locked = False
Cells(z.Row + 1, "K").Locked = False
Cells(z.Row + 1, "M").Locked = False
If Cells(Application.ActiveCell.Row, 17).Value <> "" Then Copyemail 'Q
If Cells(Application.ActiveCell.Row, 18).Value <> "" Then ThisWorkbook.Save 'R
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
Target.Rows.EntireRow.Locked = True
Else
Cells(z.Row, "M").Value = ""
End If
End If
Case Else
End Select
Next z
End If
Application.EnableEvents = True
End Sub
All I need is to make sure when the user enters YES, the cell in column B in the next row is the next cell ready for data input. Any ideas?
Thank you,
Jim Lemieux