I have a spreadsheet which uses filters (as I need to sort by company, date or status at differing times) and I want to move the entire row(s) when the status changes from either "draft" or "pending" to "Rejected" or "No Response" within the status column onto a second sheet so that I create an archive.
The code, amended as necessary to suit, should do what you want. That is, upon the user entering (or selecting from Data a Validation drop-down list) either "Rejected" or "No Response" within the status column (which must be within the spreadsheet range for which a Defined Name "rngtrigger" has been created) the entire row will be moved onto a second sheet.
As mentioned in my previous post, you must create the two Defined Names in Excel otherwise the two variables declared in the code (rngTrigger and rngDest) have nothing to work with. You must also ensure that the references in the code to the sheet objects match the actual sheet/tab names.
Also, is it possible to automatically change the status to "No Response" when the date within the deadline column lapses by a calendar month ?
I'm sure it can - would require separate but similar bit of code (probably within a different Worksheet event) to continually review the deadline column for dates less than one month earlier than today which would move the relevant row if the condition is true. Get this initial one working first then we can look at this request.
Assuming you've created the two Defined Names mentioned above, the underlined bits in the following lines may be where your current problem lies:
1.
Code:
Set rngTrigger = [U]Sheet1("Sheet1")[/U].Range("rngTrigger")
Set rngDest = [U]Sheet2("Sheet2")[/U].Range("rngDest")
These are slightly different to what you had immediately after making the change Michael M suggested. If you follow Michael's suggestion (referencing tab names for the sheet objects rather than VBA code names) then the references MUST match the corresponding tab names of the sheets in your workbook. So rather than Sheet1("Sheet1") and Sheet2("Sheet2") you should have Sheets("Sheet1") and Sheets("Sheet2") respectively, viz;
Code:
Set rngTrigger = [U]Sheets("Sheet1")[/U].Range("rngTrigger")
'Where Sheet1 is the tab name of the sheet on which Defined Name "rngTrigger" is located
Code:
Set rngDest = [U]Sheets("Sheet2")[/U].Range("rngDest")
'Where Sheet2 is the tab name of the sheet on which Defined Name "rngDest" is located
2.
Code:
If UCase(Target) = "Rejected" Or UCase(Target) = "Q No [B][U]Reponse[/U][/B]" Then
Spelling! "Q No Reponse" should be "Q No Response". The words/phrase MUST match exactly what will be entered in status column of spreadsheet, and vice versa.
3.
Code:
' Reset.EnableEvents = True
This appears to be a combination of the explanation comment and the active code line, and in any event, has no effect as it has been commented out by prefixing the line with '.The active line should be
Code:
Application.EnableEvents = True
to turn the VBA Event Handler back on so that the next change in the worksheet will trigger the code. Without resetting the Event Handler, the macro is effectively switched off!
Try the following corrected code (which
doesn't cater for your request for an automated change of status to "No Response" when the date within the deadline column lapses by a calendar month):
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
[INDENT=2]Dim rngTrigger As Range
Dim rngDest As Range
Set rngTrigger = Sheets("Sheet1").Range("rngTrigger") [I]'<< amend "Sheet1" to match actual tab name
[/I]Set rngDest = Sheets("Sheet2").Range("rngDest")[I]'<< amend "Sheet2" to match actual tab name[/I]
'Limit the trap area to range of cells in which the status "Rejected" or "Q No Response" is entered
If Not Intersect(Target, rngTrigger) Is Nothing Then
[/INDENT]
[INDENT=3]'Only trigger if status entered is "Rejected" OR "Q No Response"
If UCase(Target.value) = "Rejected" Or UCase(Target.value) = "Q No Response" Then
[/INDENT]
[INDENT=4]' Ensure subsequent deletion of 'moved' row does NOT cause the Change Event to run again and get itself in a loop!
Application.EnableEvents = False
' Do the move
Target.EntireRow.Select
Selection.Cut
rngDest.Insert Shift:=xlDown
Selection.Delete
'Reset EnableEvents
Application.EnableEvents = True
[/INDENT]
[INDENT=3]End If[/INDENT]
[INDENT=2]End If
[/INDENT]
End Sub