If Target And Or Then - Pointers?

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
Hey there guys,

I am trying to get this worksheet_Change to work correctly for what I need. I am pretty tired so I am probably not thinking straight.

I have a drop-down list in column F, known as Status, it allows me to select a Pending, Pass, Fail, or leave the cell blank as the input. In column G, known as Date Completed, I am trying to input a date automatically when the contents of column F are changed to either Pass or Fail.

I can get a date in G if anything in column F changes, but I only want it to show the date when column F is either Pass or Fail. I just can't seem to get it. Too many long nights and not enough sleep lately.

Here is the code I am using:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)    If (Target.Count = 6) And _
       (Not Intersect(Target, [G:G]) Is Nothing) Then _
        Target.Offset(0, 1) = Date
End Sub

I thought that I had it when I tried:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)    If (Target.Count = 6) And _
       (Target = "Pass" OR Target = "Fail") And _
       (Not Intersect(Target, [G:G]) Is Nothing) Then _
        Target.Offset(0, 1) = Date
End Sub

But that was a no go.

Any thoughts or advise to point me in the right direction?

Thank you everyone!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Sorry everyone,

Let me fix my code here:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

If (Target.Count = 1) And _
       (Not Intersect(Target, [F:F]) Is Nothing) Then _
        Target.Offset(0, 1) = Date
End Sub
 
Upvote 0
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    If Target.Count = 1 And Target.Column = 6 Then
        If Target.Value = "Pass" Or Target.Value = "Fail" Then
            Target.Offset(0, 1) = Date
        End If
    End If


End Sub
 
Upvote 0
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    If Target.Count = 1 And Target.Column = 6 Then
        If Target.Value = "Pass" Or Target.Value = "Fail" Then
            Target.Offset(0, 1) = Date
        End If
    End If


End Sub

Thanks there Fluff for the assistance. So the code works if I manually type in Pass or Fail the cell. However, I have a data validation dropdown list on those cells where I can pick and choose from Pending, Pass, Fail, or leave it blank. If I select Pass or Fail from the dropdown list, the date does not auto-populate. Any ideas?

Also, now that I have some sleep (very much needed), I think I might tackle this another way. I am going to explore both avenues and see what I can do. Your assistance is very much appreciated. If you have any thoughts or ideas regarding why the use of the data validation is not registering as a change in the target column, please feel free to shoot them my way.

Thanks again!

-Spydey
 
Upvote 0
What type of drop-down are you using?
 
Upvote 0
Ok, so I figured out why the data validation wasn't working. The "Pass" & "Fail" in the code is case sensitive. I have "PASS" and "FAIL" for my data validation options, where as the code is "Pass" & "Fail". I think that I can add a few OR statements to the code and and have it work for any spelling of the words.

Something like this:

Code:
If Target.Value = "Pass" Or Target.Value = "PASS" or Target.Value = "pass" or Target.Value = "Fail" or Target.Value = "FAIL" or Target.Value = "fail" Then

Unless there is a way to have it coded so that case is not looked at.

-Spydey
 
Last edited:
Upvote 0
You can use this
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    If Target.Count = 1 And Target.Column = 6 Then
        If UCase(Target.Value) = "PASS" Or UCase(Target.Value) = "FAIL" Then
            Target.Offset(0, 1) = Date
        End If
    End If

End Sub
Ucase converts the target value into Upper case.
 
Upvote 0
Ok, so I added modified a few lines of code and now it works great! Also, if the status of pass or fail is every changed, then the date is cleared out.

Here is the code:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    If Target.Count = 1 And Target.Column = 6 Then
        If Target.Value = "Pass" Or Target.Value = "PASS" Or Target.Value = "pass" Or Target.Value = "Fail" Or Target.Value = "FAIL" Or Target.Value = "fail" Then
            Target.Offset(0, 1) = Date
        Else
            Target.Offset(0, 1).ClearContents
        End If
    End If


End Sub
 
Upvote 0
You can use this
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    If Target.Count = 1 And Target.Column = 6 Then
        If UCase(Target.Value) = "PASS" Or UCase(Target.Value) = "FAIL" Then
            Target.Offset(0, 1) = Date
        End If
    End If

End Sub
Ucase converts the target value into Upper case.

I will give that a try, thanks for your assistance Fluff, you have been most generous.

-Spydey
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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