Feb 21 2024 06:51 AM
Hello,
I currently have an Excel spreadsheet and code to automatically move a row when I change the status in a dropdown to "completed" to move the row from Sheet1 into the Completed Sheet.
The problem is that the completed items start at the top of the Completed Sheet and go down from oldest to newest when I would like the completed items to have the newest at the top going to oldest of the Completed Sheet. Is there a way to update my code, or add a code to accomplish this?
Feb 22 2024 01:39 AM
The image you sent appears to show code related to moving rows based on a dropdown value in Excel. Here's how you can modify your code to populate the "Completed" sheet with the newest entries at the top:
Option 1: Using Insert Rows and MoveRow (simpler method):
Sub MoveRowsToCompleted() Dim sourceSheet As Worksheet Dim targetSheet As Worksheet Dim lastRow As Long Dim i As Long ' Set source and target sheets Set sourceSheet = Worksheets("Sheet1") Set targetSheet = Worksheets("Completed") ' Get the last row in the target sheet (assuming no headers) lastRow = targetSheet.UsedRange.Rows.Count ' Loop through rows in the source sheet starting from the bottom For i = sourceSheet.UsedRange.Rows.Count To 2 Step -1 ' Check if the dropdown value in column B is "completed" If sourceSheet.Cells(i, 2).Value = "completed" Then ' Insert a new row at the top of the target sheet targetSheet.Rows(1).Insert xlUp ' Move the row from the source sheet to the target sheet sourceSheet.Rows(i).Move targetSheet.Rows(1) ' Decrement the counter to avoid skipping rows i = i - 1 End If Next i End Sub
Explanation:
Option 2: Using CopyRows and Sort (more efficient for large datasets):
Sub MoveRowsToCompleted() Dim sourceSheet As Worksheet Dim targetSheet As Worksheet Dim lastRow As Long ' Set source and target sheets Set sourceSheet = Worksheets("Sheet1") Set targetSheet = Worksheets("Completed") ' Get the last row in the target sheet (assuming no headers) lastRow = targetSheet.UsedRange.Rows.Count ' Copy all rows from the source sheet that meet the criteria Dim filteredRange As Range Set filteredRange = sourceSheet.UsedRange.Find(What:="completed", LookIn:=xlValues, SearchOrder:=xlByRows) If Not filteredRange Is Nothing Then filteredRange.Copy targetSheet.Rows(lastRow + 1) End If ' Sort the target sheet based on a column indicating completion date (adjust column number as needed) targetSheet.UsedRange.Sort key1:=targetSheet.Columns("C"), SortOrder:=xlDescending ' Assuming completion date is in column C End Sub
Explanation:
Choose the option that best suits your needs and coding preferences. Remember to adjust the code to match your specific sheet names, column references, and any other relevant modifications
Feb 22 2024 01:47 AM
@LenaGonzo Rather than using a macro, why not add a status column to your data, convert your data range to a table (Insert, Table) and then add a slicer to your table for the Status column. That makes it very easy to filter for the status.
Feb 22 2024 07:52 AM
Feb 22 2024 07:53 AM
Feb 22 2024 08:40 AM
@LenaGonzo No, it changes your current layout as it obsoletes the completed worksheet. I just wanted to make you look at this from a different perspective.