VBA help - I can't move data to last row of different sheet

aliecat08

New Member
Joined
Oct 18, 2017
Messages
14
Office Version
  1. 2013
Platform
  1. Windows
Complete VBA newbie - 6 years ago, I tried making an Excel workbook to track cases and automatically move cases from "OpenCases" to "ClosedCases" when case status changed to "Closed". I found this code on some site (maybe even here?) and just changed the triggering event to "CLOSED". It does the job of moving the row but it's inserting in the ClosedCases sheet in weird places. Sometimes row 4 sometimes row 3. It's so weird.

I gave up on the project but now we need to use it again so I'm trying to fix the issue. I tried changing the range of the destination row in name manager, but then the code stopped working.

Anyway, I need it tweaked so it automatically transfers the data to the last row available row on "Closed Cases". I also need to sort "ClosedCases" alphabetically, so every time the case is moved from Open to Closed, it should automatically sort the sheet upon closing the workbook.

Here is the code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDest As Range
Set rngDest = Sheet2.Range("rngDest")
' Limit the trap area to range of cells in which completed dates are entered as defined above
If Not Intersect(Target, Sheet1.Range("rngTrigger")) Is Nothing Then
' Only trigger if the value entred is a date or is recognizable as a valid date
     If UCase(Target) = "CLOSED" Then
'Ensure subsequent deletion of 'moved' row does NOT cause the Change Event to run again and get itself in a loop!
        Application.EnableEvents = False
        Target.EntireRow.Select
        Selection.Cut
        rngDest.Insert Shift:=xlDown
        Selection.Delete
' Reset EnableEvents
        Application.EnableEvents = True
    End If
End If
End Sub


NOTE - In the name manager, "rngTrigger" =OpenCases!$G:$G and "rngDest" =ClosedCases!$3:$3. Row 1 has the headers but isn't named.


PLEASE PLEASE HELP!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hello Aliecat,

Could you upload a sample of your workbook using the Xl2BB function (noted at the top of any reply box).
It's probably simpler to not use the named ranges in your case.
What column do you want to sort on? Are they surnames in the sort column?

Cheerio,
vcoolio.
 
Upvote 0
This is my first time using XI2BB. Did it work? In the OpenCases sheet, I want that entire row to move to ClosedCases when the "Status" column is changed to "Settled" or "Dropped" or "Closed". In the ClosedCases sheet, I want it autosorted by the "Last Name" column.

Moving Closed Status to ClosedCases Sheet.xlsm
ABCDEFG
1LAST NAMEFIRST NAMEDOLSTATUTE ATTORNEY(S)ASSISTANTSTATUS
2DOEALPHA3/8/20213/8/2023RomeoKiloPre-lit
3DOEBRAVO4/28/20214/28/2023RomeoJuliettPre-lit
4DOECHARLIE9/30/20229/30/2024LimaPapaFiled
5DOEDELTA9/13/20219/13/2023LimaPapaPre-lit
6DOEECHO10/18/202010/18/2022MikeQuebecFiled
7DOEFOXTROT2/18/20212/18/2023MIkeQuebecPre-lit
8DOEGOLF12/1/202212/1/2018Yankee UniformFiled
9DOEHOTEL5/10/20215/10/2023Yankee VictorPre-lit
10DOEINDIA5/19/20215/19/2023RomeoWhiskeySettled
11DOEINDIA6/9/20216/9/2023RomeoWhiskeySettled
12PITTBRAD1/1/20181/1/2020LimaVictor
OpenCases



Moving Closed Status to ClosedCases Sheet.xlsm
ABCDEFGH
1LAST NAMEFIRST NAMEDOLSTATUTE ATTORNEY(S)ASSISTANTSTATUSRESOLUTION TYPE
2SMITHJOHN1/29/20211/29/2023RomeoWhiskeyCLOSED
3DOEINDIA5/19/20175/19/2019RomeoWhiskeyClosed
4BRADY TOMTOM1/30/20211/30/2023ROMEOWhiskeyCLOSED
5DOEALPHA3/8/20173/8/2019RomeoKiloClosedSettled
6DOEBRAVO4/28/20174/28/2019RomeoJuliettClosedSettled
7DOECHARLIE9/30/20169/30/2018LimaPapaClosedSettled
8DOEDELTA9/13/20179/13/2019LimaPapaClosedDropped
9DOEECHO10/18/201410/18/2016MikeQuebecClosedSettled
10DOEFOXTROT2/18/20172/18/2019MIkeQuebecClosedSettled
11DOEGOLF12/1/201612/1/2018Yankee UniformClosedDropped
12DOEHOTEL5/10/20175/10/2019Yankee VictorClosedSubbed-Out
13DOEINDIA5/19/20175/19/2019RomeoWhiskeyClosedDropped
ClosedCases
 
Upvote 0
Hello Aliecat,

Thanks for supplying the sample.

Try the following codes in a copy of your workbook first:-

VBA Code:
Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Columns(7)) Is Nothing Then Exit Sub
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Value = vbNullString Then Exit Sub
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
        If Target.Value = "Closed" Or Target.Value = "Settled" Or Target.Value = "Dropped" Then
             Target.EntireRow.Copy Sheets("ClosedCases").Range("A" & Rows.Count).End(3)(2)
             Target.EntireRow.Delete
             Sheets("ClosedCases").Columns.AutoFit
        End If
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

The above code should do the copy/pasting task for you once the relevant criteria is typed into the the relevant cell in the Status column. If your Status criteria are selected from a drop down, the code will trigger instantly otherwise, just click away from the cell. The code will also delete the relevant row of data from the OpenCases worksheet once the data transfer is completed.

To implement this code into your workbook:-
- Right click on the OpenCases tab and select "View Code" from the menu that appears.
- In the big white code field that then appears, paste the above code.

The following code will sort your data in the ClosedCases worksheet once the workbook is closed:-

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

If Cancel = True Then Exit Sub
Dim wsCC As Worksheet: Set wsCC = Sheets("ClosedCases")

wsCC.[A1].CurrentRegion.Offset(1).Sort wsCC.[A2], 1

End Sub

The code will sort the data 'Ascending' based on Column A.

To implement this code into the ThisWorkbook module:-
- Right click on any tab and select "View Code".
- Over to the left in the Project Explorer, double click on ThisWorkbook.
- In the big white code field that appears, paste the above code.

Close and save the workbook.

I hope that tis helps.

Cheerio,
vcoolio.
 
Upvote 0
Solution
Omg, this was perfect. THANK YOU!!!

Is there a way to do the reverse, in case we mistakenly move a case to ClosedCases sheet. So if the "Status" in "ClosedCases" is changed from "Closed" or "Settled" or "Subbed Out" (it'll be something like "Filed" "Pending" or "Pre-lit" it will move it back to the OpenCases sheet?

And ideally, add a message that pops up before the move that confirms the move. Something like "Are you sure you want to move the case to Closed Cases?" and "Are you sure you want to move the case to Open Cases?"
 
Upvote 0
Hello Aliecat,

To allow for a message box to pop up prior to any data transfer, change the WorkSheet_Change code to:-

VBA Code:
Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Columns(7)) Is Nothing Then Exit Sub
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Value = vbNullString Then Exit Sub
   
    Application.ScreenUpdating = False
    Application.EnableEvents = False
       
        If Target.Value = "Closed" Or Target.Value = "Settled" Or Target.Value = "Dropped" Then
        If MsgBox("Are you sure that you have selected the correct case to move to Closed Cases?", vbCritical + vbYesNo, "WARNING") = vbYes Then
             Target.EntireRow.Copy Sheets("ClosedCases").Range("A" & Rows.Count).End(3)(2)
             Target.EntireRow.Delete
             Sheets("ClosedCases").Columns.AutoFit
        End If
        End If
       
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

To send data back to the Open Cases worksheet should a case be mistakenly sent to the Closed Cases worksheet, place the following code into the worksheet module of the ClosedCases tab. Implement it as per the instrucrions for the OpenCases worksheet in post #4.
VBA Code:
Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Columns(8)) Is Nothing Then Exit Sub
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Value = vbNullString Then Exit Sub
   
    Application.ScreenUpdating = False
    Application.EnableEvents = False
       
        trow = Target.Row
        If Target.Value = "Yes" Then
        If MsgBox("Are you sure that you have selected the correct case to move back to Open Cases?", vbCritical + vbYesNo, "WARNING") = vbYes Then
             Range(Cells(trow, "A"), Cells(trow, "F")).Copy Sheets("OpenCases").Range("A" & Rows.Count).End(3)(2)
             Target.EntireRow.Delete
             Sheets("OpenCases").Columns.AutoFit
        End If
        End If
       
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

I've tried to simplify it for you by using column 8("H") in the Closed Cases worksheet as the criteria column but only using one criteria, "Yes". A message box will pop up asking the User if they want to proceed once "Yes" is entered in a relevant cell. You can use another criteria column for this if you like. Let us know what you think.
Again, the relevant row of data will be deleted from the Closed Cases work sheet once the data is transferred back to the Open Cases worksheet.

Cheerio,
vcoolio.
 
Upvote 0
You're welcome Aliecat. I'm glad to have been able to assist and thanks for the feed back.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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