VBA Help

ashani

Active Member
Joined
Mar 14, 2020
Messages
345
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm using the following coding to bring date to the next column. However, I'd also like that if J21:J22 has N/A then it should show N/A in column K rather than a date.

If Not Intersect(Target, Range("J21:J22")) Is Nothing Then
If Target.Count > 1 Then Exit Sub
ActiveSheet.Unprotect Password:="test"
Cells(Target.Row, "K").Value = Format(Now, "dd/mm/yyyy")
ActiveSheet.Protect Password:="test"

THank you
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How is this script activated? Is this a module script or a sheet change event script?
Show us the entire script
 
Upvote 0
hi @My Aswer Is This

Thank you for the reply.

Here's the full script :

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("J21:J22")) Is Nothing Then
If Target.Count > 1 Then Exit Sub
ActiveSheet.Unprotect Password:="test"
Cells(Target.Row, "K").Value = Format(Now, "dd/mm/yyyy")
ActiveSheet.Protect Password:="test"
End if

end sub
 
Upvote 0
How about
VBA Code:
If Not Intersect(Target, Range("J21:J22")) Is Nothing Then
If Target.Count > 1 Then Exit Sub
ActiveSheet.Unprotect Password:="test"
If Target.Value = "N/A" Then Target.Offset(, 1).Value = "N/A"
Target.Offset(, 1).Value = Format(Date, "dd/mm/yyyy")
ActiveSheet.Protect Password:="test"
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  11/27/2021  11:18:25 AM  EST
If Not Intersect(Target, Range("J21:J22")) Is Nothing Then
If Target.Count > 1 Then Exit Sub
ActiveSheet.Unprotect Password:="test"

Select Case Target.Value
    Case "N/A"
        Cells(Target.Row, "K").Value = "NA"
    
    Case Else
        Cells(Target.Row, "K").Value = Format(Now, "dd/mm/yyyy")
ActiveSheet.Protect Password:="test"
End Select
End If

End Sub
 
Upvote 0
If the values in J21:J22 are a result of formulas, the Worksheet Change event procedure does not trigger on formula calculations. It only triggers off of manual changes to the sheet.

You could trigger off of the cells you do change, but test the results of the formulas in J21:J22. In the example below, the code triggers off of changes in I21:I22, tests the result in the corresponding J column, then writes the Date or #N/A to column K.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("I21:I22")) Is Nothing Then
        ActiveSheet.Unprotect Password:="test"
        If Application.IsNA(Cells(Target.Row, "J").Value) Then
            Cells(Target.Row, "K").Value = CVErr(xlErrNA)
        Else
            Cells(Target.Row, "K").Value = Date
            Cells(Target.Row, "K").NumberFormat = "dd/mm/yyyy"
        End If
        ActiveSheet.Protect Password:="test"
    End If
End Sub
 
Upvote 0
Thank you for your help everyone. Really appreciate that.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,039
Members
449,063
Latest member
ak94

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