I'm trying to get a macro to operate by calling out another macro to unlock a worksheet to allow a RFID reader to scan and input data and then protect the document again to allow it from being edited.
Private Sub Worksheet_Change(ByVal Target As Range)
' call another macro
Call UnProtect
' End Sub
' Dim i As Integer
' MsgBox (Target.Row & ":" & Target.Column)
' For i = 8 To 200
If Target.Column = 3 And Target.Row <= 12 Then
If Cells(Target.Row, Target.Column).Value <> "" And Cells(Target.Row, "E").Value = "" Then
' Cells(Target.Row, "E").Value = Now()
' Cells(Target.Row, "E").NumberFormat = "h:mm AM/PM"
End If
End If
' Next
' Range("E:E").EntireColumn.AutoFit
' End Sub
' Private Sub Worksheet_Change(ByVal Target As Range)
' Dim i As Integer
' MsgBox (Target.Row & ":" & Target.Column)
' For i = 8 To 200
If Target.Column = 1 And Target.Row <= 17 Then
If Cells(Target.Row, Target.Column).Value <> "" And Cells(Target.Row, "B").Value = "" Then
Cells(Target.Row, "B").Value = Now()
' Cells(Target.Row, "F").NumberFormat = "h:mm AM/PM"
End If
End If
' Next
' Range("F:F").EntireColumn.AutoFit
' End Sub
' Private Sub Worksheet_Change(ByVal Target As Range)
' Dim i As Integer
' MsgBox (Target.Row & ":" & Target.Column)
' For i = 8 To 200
If Target.Column = 3 And Target.Row >= 15 Then
If Cells(Target.Row, Target.Column).Value <> "" And Cells(Target.Row, "D").Value = "" Then
Cells(Target.Row, "D").Value = Now()
Cells(Target.Row, "D").NumberFormat = "mm/dd/yyy"
End If
End If
' Next
Range("D:D").EntireColumn.AutoFit
' End Sub
' call another macro
Call Protect
End Sub
The two macros it is calling to are
Sub UnProtect()
'Unprotect a worksheet
Sheets("Sign in Sheet").UnProtect
End Sub
and
Sub Protect()
'Protect a worksheet
Sheets("Sign in Sheet").Protect
End Sub
However, when I only include the unlock macro, the code will work fine. But when I add the protect macro, an error code will come up
Run-Time Error '1004':
Unable to set the NumberFormat property of the Range class
and it points to
Cells(Target.Row, "D").NumberFormat = "mm/dd/yyyy"
Any ideas as to what exactly is happening.
I see no provision for a case when Target is more than a single cell and there's a pretty good chance that the Worksheet_Change is trying to run on top of itself.
Cycle through each range object in Target and disable event triggers.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo safe_exit
Application.EnableEvents = False
Call Unprotect
Dim t As Range
If Not Intersect(Target, Range("A1:A17")) Is Nothing Then
For Each t In Intersect(Target, Range("A1:A17"))
If t.Value <> vbNullString And t.Offset(0, 1).Value <> vbNullString Then
t.Offset(0, 1) = Now
t.Offset(0, 1).NumberFormat = "h:mm AM/PM"
End If
Next t
End If
If Not Intersect(Target, Range("C1:C12")) Is Nothing Then
For Each t In Intersect(Target, Range("C1:C12"))
If t.Value <> vbNullString And t.Offset(0, 2).Value <> vbNullString Then
t.Offset(0, 2) = Now
t.Offset(0, 2).NumberFormat = "h:mm AM/PM"
End If
Next t
'I don't understand why column F comes into play here
' Range("F:F").EntireColumn.AutoFit
End If
If Not Intersect(Target, Range("C15:C999999")) Is Nothing Then
For Each t In Intersect(Target, Range("C15:C999999"))
If t.Value <> vbNullString And t.Offset(0, 1).Value <> vbNullString Then
t.Offset(0, 1) = Now
t.Offset(0, 1).NumberFormat = "mm/dd/yyy"
End If
Next t
Range("D:D").EntireColumn.AutoFit
End If
Call Protect
safe_exit:
Application.EnableEvents = True
End Sub
Related
I have two working Worksheet_Change code and I would like to use both of them on the same sheet. When I use them individually both of them work but when I use them together they do not. I tried to paste in two different codes but I got an ambiguous name detected error. I also tried to use elseif, next but none of them worked.
The two codes:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("O:O"), Target) Is Nothing Then
Cells(Target.Row, 17).Value = Date
End If
End Sub
and
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 15 Then
Range("P" & Target.Row).Value = Target.Value + Range("P" & Target.Row).Value
Application.EnableEvents = False
Target.Value = ""
Application.EnableEvents = True
End If
End Sub
Thank you for your help
you can just put both workshett Change events in the same sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("O:O"), Target) Is Nothing Then
Cells(Target.Row, 17).Value = Date
End If
If Target.Column = 15 Then
Range("P" & Target.Row).Value = Target.Value + Range("P" & Target.Row).Value
Application.EnableEvents = False
Target.Value = ""
Application.EnableEvents = True
End If
End Sub
Try it like this...
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Range("O:O"), Target) Is Nothing Then
Application.EnableEvents = False
Cells(Target.Row, 17).Value = Date
Range("P" & Target.Row).Value = Target.Value + Range("P" & Target.Row).Value
Target.Value = ""
Application.EnableEvents = True
End If
End Sub
Is there a way I can automate the date or time once the I enter the data from the User_Form via "enter/click" button? I've tried this code but it keeps restarting my excel workbook. On top of that, I have a ton of other codes in the Private Sub Worksheet_Change(ByVal Target As Range)
Is it possible it's overloaded ?
So I'm thinking if I could code it with my Userform_click() I'd be better off?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 16 To 100
If Cells(i, 3).Value <> "" Then
Cells(i, 1).Value = Date & " "
Cells(i, 1).NumberFormat = "mm/dd/yy"
End If
Next
End Sub
You need to add Application.EnableEvents = False in the beginning of your Sub, otherwise it will keep running it every time a value is changed inside the worksheet (like when you change it inside your For i = 16 To 100 loop).
Code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Application.EnableEvents = False
For i = 16 To 100
If Cells(i, 3).Value <> "" Then
Cells(i, 1).Value = Date & " "
Cells(i, 1).NumberFormat = "mm/dd/yy"
End If
Next i
Application.EnableEvents = True '<-- restore to original setting
End Sub
Edit 1: You can write your Sub in another way, that it will enter it only if a cell is changed inside the searched Range("C16:C100"). Only if the modified cell is inside that range, then check each cell if the Value <> "".
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
Application.EnableEvents = False
If Not Intersect(Range("C16:C100"), Target) Is Nothing Then
For Each C In Intersect(Range("C16:C100"), Target)
If C.Value <> "" Then
C.Offset(, -2).Value = Date & " "
C.Offset(, -2).NumberFormat = "mm/dd/yy"
End If
Next C
End If
Application.EnableEvents = True
End Sub
Currently I have this code. In Column A, I have a current a "YES" or "No" Selection.
Private Sub worksheet_change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
ActiveSheet.Unprotect
If Target = "YES" Then
'Column B to S
For i = 1 To 18
With Target.Offset(0, i)
.Locked = False
.FormatConditions.Add Type:=xlExpression, Formula1:="=ISBLANK(" & Target.Offset(0, i).Address & ")"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
.Interior.ColorIndex = 4
End With
End With
Next i
ElseIf Target = "NO" Then
For i = 1 To 73
With Target.Offset(0, i)
.Value = ""
.Locked = True
.FormatConditions.Delete
End With
Next i
End If
ActiveSheet.Protect
End If
End Sub
Now when the user click the cell in Column T (19), I want to display a warning message to the user that this is not applicable for "Yes" selection.
This seems like it should do the task you are asking.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
On Error GoTo bm_SafeExit
Application.EnableEvents = False
Me.Unprotect
Dim trgt As Range
For Each trgt In Intersect(Target, Range("A:A"))
If LCase(trgt.Value2) = "yes" Then
With trgt.Offset(0, 1).Resize(1, 18)
.Locked = False
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=ISBLANK(B" & trgt.Row & ")")
.Interior.ColorIndex = 4
End With
End With
Else
With trgt.Offset(0, 1).Resize(1, 73)
.Value = vbNullString
.Locked = True
.FormatConditions.Delete
End With
End If
Next trgt
End If
bm_SafeExit:
Application.EnableEvents = True
Me.Protect Userinterfaceonly:=True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("T:XFD")) Is Nothing Then
On Error GoTo bm_SafeExit
Application.EnableEvents = False
Dim trgt As Range
For Each trgt In Intersect(Target, Range("T:XFD"))
If LCase(Me.Cells(trgt.Row, "A").Value2) = "yes" Then
MsgBox "Don't try to put Yes here", vbCritical + vbOKOnly, "Bad Choice"
Me.Cells(trgt.Row, "A").Select
End If
Next trgt
End If
bm_SafeExit:
Application.EnableEvents = True
End Sub
Set watches and breakpoints and use [F8] and [Ctrl]+[F8} to walk through the code.
I am creating an Excel spreadsheet. I have 2 separate functions that I need to combine but I am not sure how to smash them together. I know I can only have 1 change event. The first function will unprotect the sheet (column c is locked), auto populate column C when data is entered in to column A or erase C when A is erased and re-protect when complete. The second will return the cell focus to the next row, column A, when data is entered into A and B. Separately they work as needed.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Unprotect Password:="my password"
If Target.Column = 1 Then
Dim A As Range, B As Range, Inte As Range, r As Range
Set A = Range("A:A")
Set Inte = Intersect(A, Target)
If Target.Offset(0, 1 - Target.Column).Value = "" Then
Target.Offset(0, 3 - Target.Column).Clear
Exit Sub
End If
Application.EnableEvents = False
For Each r In Inte
r.Offset(0, 2).Value = Date & " " & Time
r.Offset(0, 2).NumberFormat = "m/d/yyyy h:mm am/pm"
Next r
Application.EnableEvents = True
End If
Protect Password:="my password"
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
If Not Target.Cells.CountLarge > 1 Then
If Not Intersect(Target, Columns(1)) Is Nothing Then
Target.Offset(, 1).Select
ElseIf Not Intersect(Target, Columns(2)) Is Nothing Then
Target.Offset(1, -1).Select
End If
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
How about this, seems to do what you want, as I understand the question.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngIntersect As Range
Dim rngCell As Range
On Error GoTo TidyUp
Application.EnableEvents = False
If Target.Column = 1 Then
Set rngIntersect = Intersect(Range("A:A"), Target)
For Each rngCell In rngIntersect
If rngCell.Value = "" Then
rngCell.Offset(0, 2).Value = ""
Else
rngCell.Offset(0, 2).Value = Date & " " & Time
rngCell.Offset(0, 2).NumberFormat = "m/d/yyyy h:mm am/pm"
End If
Next rngCell
End If
If Target.Column < 3 And Target.Value <> "" Then ' lose the 'And Target.Value <> ""' as desired
Cells(Target.Row + Target.Rows.Count, 1).Select
End If
TidyUp:
Set rngIntersect = Nothing
Set rngCell = Nothing
Application.EnableEvents = True
End Sub
I'd also suggest using UserInterfaceOnly in your worksheet.Protect, then you don't have to unprotect the sheet for VBA to act on the sheet.
Implement it in two Sub-Procedures on a modul, then just call both of them in the Event-Procedure.
My codes gives me a Object Required 424 error on this line:
lngRow = .Cells(.Rows.Count, 1).End(xlUp).Row
My full code:
Private Sub Worksheet_Change(ByVal Target As Range)
' If Target.Count > 1 Then Exit Sub
' If Target.Column > 2 Then Exit Sub
Application.EnableEvents = False
If Target.Column = 6 Then
If Target.Offset(0, 1).Value <> "" Then
MsgBox "You must only fill in one of the two columns"
Target.ClearContents
GoTo ExitSub
End If
End If
If Target.Column = 7 Then
If Target.Offset(0, -1).Value <> "" Then
MsgBox "You must only fill in one of the two columns"
Target.ClearContents
GoTo ExitSub
End If
End If
Dim arrData() As Variant
Dim i As Long
Dim lngRow As Long
Dim myNum As Variant
Dim ws As Worksheet
myNum = Target.Value
If Target.Column = 6 Then
With BogieInspectionPoints 'this is a sheet name
lngRow = .Cells(.Rows.Count, 1).End(xlUp).Row
arrData = .Range("a1:b" & lngRow)
End With
End If
If Target.Column = 7 Then
With WagonInspectionPoints 'this is a sheet name
lngRow = .Cells(.Rows.Count, 1).End(xlUp).Row
arrData = .Range("a1:b" & lngRow)
End With
End If
For i = 1 To lngRow
If myNum = arrData(i, 1) Then
Cells(Target.Row, 8).Value = arrData(i, 2)
Exit For
End If
Next
ExitSub:
Application.EnableEvents = True
End Sub
It looks like those sheet variables aren't set.
You will need to add this at the top.
Dim BogieInspectionPoints as Worksheet
Dim WagonInspectionPoints as Worksheet
Set BogieInspectionPoints = ActiveWorkbook.Sheets("BogieInspectionPoints")
Set WagonInspectionPoints = ActiveWorkbook.Sheets("WagonInspectionPoints")
I was assuming there was other code. When you add this line all the With statements should process correctly using the code you posted.
What you're doing with the With statements is shorthanding the object. Instead of writing
BogieInspectionPoints.Range("A1")
'More code
You can write
With BogieInspectionPoints
.Range("A1")
End With
It keeps you from having to write the full object name out.