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:
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]
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]