Sorting data after moving a row in from another sheet

thegillimonster

New Member
Joined
May 10, 2019
Messages
3
Hello!

Disclaimer: Extremely new to VBA. Not computer-illiterate, but not familiar with VBA.

I have a table with action items containing a column which marks each item as Open or Complete. My end-state is to move rows in the "Open" sheet into the "Complete" sheet as soon as the action item is changed from "Open" to "Complete". I derived this code from a post here so thank you to wherever I got that from, it works great :).

After the copy and paste, I want the code to then sort the pasted data so that the action item # continues to line up. For example, if items 1,2,3 are marked complete in the order 2,1,3, they will appear in the "Complete" sheet in the order they were marked. I want the second step of my code to then sort the data so that the items in the "Complete" sheet follow the correct 1,2,3 order. This section of my code was done using a macro, but it fails when I try to shoehorn it into my old code. The glaring error that I see is .SetRange Range("A2:G11"), because that range will change each time a new row is added and the code does not account for this.

My code as it exists currently looks like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Version 2
'Move Action item from Open list to Complete list

'Set target row in Complete sheet by finding last row and adding 1 
If Not Intersect(Target, Range("F:F")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Dim Lastrow As Long
Lastrow = Sheets("Complete").Cells(Rows.Count, "F").End(xlUp).Row + 1

'Copy and paste newly marked Complete row from Open sheet into Complete sheet
If Target.Value = "Complete" Then
Rows(Target.Row).Copy Destination:=Sheets("Complete").Rows(Lastrow)
Rows(Target.Row).Delete

'Macro-derived formula to select and sort proper row in Complete sheet. This part is not working.
    Sheets("Complete").Select
    Range("A2").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Complete").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Complete").Sort.SortFields.Add Key:=Range("A2"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Complete").Sort
        .SetRange Range("A2:G11")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End If
End If
End Sub

Data in the "Open" sheet looks like this:
[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Title[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]#[/TD]
[TD]Action[/TD]
[TD]Topic[/TD]
[TD]Actor[/TD]
[TD]Deadline[/TD]
[TD]Status[/TD]
[TD]Comments[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Steve to do X[/TD]
[TD]BBQ[/TD]
[TD]Steve[/TD]
[TD]17 May 2019[/TD]
[TD]Open[/TD]
[TD]Steve needs Y[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Evan to buy Y[/TD]
[TD]BBQ[/TD]
[TD]Evan[/TD]
[TD]17 May 2019[/TD]
[TD]Open[/TD]
[TD]Evan needs funds[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Carl to organize baseball[/TD]
[TD]BBall[/TD]
[TD]Carl[/TD]
[TD]24 May 2019[/TD]
[TD]Open[/TD]
[TD]Need attendance list[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I have a very unelegant solution. It works, but I don't like how inefficient it is. On the "Complete" sheet, I added code to sort the data each time the sheet is clicked on.

It bothers me that I couldn't really figure it out though. How do you run code in Sheet1 that affects Sheet2? I think that is the crux of my problem.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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