0

I'm trying to delete a row in Excel 2007 via VBA if it meets two requirements: A a termination date greater than one provided (variable datesToRemoveTerminatedMembersFrom) and also if there is a "D" in column R.

The code I'm using does not give me an error, but does not delete the row either:

For i = lastRow To 2 Step -1
    If Range("R" & i).Value = "D" Then
        If Range("D" & i).Value > datesToRemoveTerminatedMembersFrom Then
            Rows(i).Delete
        End If
    End If
Next i

They both work by themselves, but not if I nest them.

Thanks for your help in advance

2
  • it works for me..Have you defined lastRow ? Commented Oct 31, 2013 at 3:51
  • Hi Sam092 lastRow was defined earlier in the script Commented Oct 31, 2013 at 5:01

2 Answers 2

1

Try below code

For i = lastRow To 2 Step -1
    If UCase(Range("R" & i).Value) = "D" Then
        If CDate(Range("D" & i).Value) > CDate(datesToRemoveTerminatedMembersFrom) Then
            Rows(i).Delete
        End If
    End If
Next
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this but it deleted everything with a D value in column R.
@Chris Add breakpoint at If CDate(Range("D" & i).Value) > CDate(datesToRemoveTerminatedMembersFrom) Then .This will show how the condition is working. Make sure the date are in same format.
0

I actually don't need this anymore. The person I was setting up the spreadsheet application for requested that I delete each record where the termination date was greater than the filedate.

What she actually meant was replace the termination date with "", not delete the row, doh.

Thanks for your help, the code I used in the end was:

For i = lastRow To 2 Step -1

        If CDate(Range("D" & i).Value) > CDate(datesToRemoveTerminatedMembersFrom) Then
            Range("D" & i) = ""
        End If

Next i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.