McCrimmon,
Here is the code description that you asked for. I hope it all makes since, but if it doesn't, please don't hesitate to ask me about anything you don't understand.
Sub MoveComplete()
'The Dim statements define what you want the variable to be.
'Undefined variable automatically become the Variant type and
'code with all Variants does not run efficiently. I have my
'VB editor set up so that it forces me to define all my variables
Dim lOTLastRow As Long, lCTLastRow As Long, lRow As Long
Dim wsOT As Worksheet, wsCT As Worksheet
'Here I am assigning the two worksheets to a variable which makes
'coding easier and also keeps me from have to use the Select
'statement which ultimately slows down your code
Set wsOT = Sheets("Outstanding Tasks")
Set wsCT = Sheets("Completed Tasks")
'These next two line find the last row with information on each
'of the sheets. I add 1 to lCTLastRow to give me the first blank
'row. This is where we begin putting the complete data.
lOTLastRow = wsOT.Cells(Rows.Count, 1).End(xlUp).Row
lCTLastRow = wsCT.Cells(Rows.Count, 1).End(xlUp).Row + 1
'This is a For Next loop and I am telling it to begin with the last
'row and work up through each row through row 7. The Step -1 is
'indicating to move upward. Step 1 would be a downward movement. I
'am moving backwards because we are deleting rows. Example: If I were
'moving down and I am on row 300, I move that row and then delete it.
'Row 301 now becomes 300 but the For Next is going to move to the next
'Row which is 301 so you will end up missing a row. If we are working
'backwards and we delete row 300, row 301 still becomes row 300 but we
'have already looked at 301 and the next row we will be looking at is 299.
For lRow = lOTLastRow To 7 Step -1
'Looking at Column 30 or AD for whatever row we are on. The UCase just
'makes sure that all the letters are capitalized which accounts for
'someone mistakenly entering Yes instead of YES.
If UCase(wsOT.Cells(lRow, 30)) = "YES" Then
'If YES is in Col 30, the whole row is copied off the Outstanding Task
'Sheet
wsOT.Range("A" & lRow).EntireRow.Copy
'Whole row is pasted onto the Completed sheet. lCTLastRow represents
'the next available blank line
wsCT.Range("A" & lCTLastRow).PasteSpecial
'Whole row is deleted of the Outstanding Task sheet
wsOT.Range("A" & lRow).EntireRow.Delete
'Next blank row on Completed sheet is advanced by one
lCTLastRow = lCTLastRow + 1
End If
'Goes to next row and loops back up to the For lRow = ...
Next lRow
End Sub
I have been programing in VBA for about 7 or 8 years now. I just recently found this forum and it is absolutely fantastic. It is amazing what I have learned and how much better my code it now. If you are not sure about something don't hesitate to post to this list. There are some very talented folks monitoring this forum and you will almost always get an answer.
I started out asking questions and then decided to see if I could answer a few. Mudraker told me that answering question helped him learn even more and he was absolutely right. I have learned all kinds of new things simply by answering questions. There are often several ways of accomplishing the same task and by asking a question you might find an easier way of doing something. So ask question!!!!
Bookmarks