VBA excel: creating a list from sheet 1 onto sheet 2

The attached file contains account information on customers. I need to write a sub that creates a list on sheet 2 of all the customers who still owe more than $1000. It should first clear the contents of any previous list on sheet 2 and must show customer IDs and the amounts owed and should work even if the amount of data changes.

I am learning excel and would prefer if the solution is simple. Thanks!!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

app clears the output sheet (sheet2), then goes through all of column A on Sheet 1, looking at amount purchased less paid.  If that amount >= $1,000, an entry is made on the output sheet, with customer ID and amount owed

Here's the code:
Sub genList()
Dim wkb As Workbook
Dim srcSht As Worksheet
Dim outSht As Worksheet
Dim rng As Range
Dim outCursor As Range
Dim lastRow As Long
Dim checkOwed As Double

    Set wkb = ThisWorkbook
    Set srcSht = wkb.Sheets("Sheet1")
    Set outSht = wkb.Sheets("Sheet2")
    'first, clear Sheet2
    lastRow = outSht.Range("A" & outSht.Rows.Count).End(xlUp).Row
    If lastRow >= 4 Then
        outSht.Range("A4", "A" & lastRow).EntireRow.ClearContents
    End If
    Set outCursor = outSht.Range("A4")
    For Each rng In srcSht.Range("A4", srcSht.Range("A" & srcSht.Rows.Count).End(xlUp))
        checkOwed = rng.Offset(0, 1).Value - rng.Offset(0, 2).Value 'amount owed - amount paid
        If checkOwed >= 1000 Then 'copy to output sheet
            outCursor.Value = rng.Value 'customer_ID
            outCursor.Offset(0, 1).Value = checkOwed 'and amount owed
            Set outCursor = outCursor.Offset(1, 0) 'proceed to next row for next customer
        End If
    Next rng
    MsgBox "Process Complete, Hit Enter to see outputs", vbOKOnly
End Sub

Open in new window

See attached workbook.

PS - this could have been done with a pivot table, but I'm assuming you're doing this to get some VBA exposure as well, so hope you found this helpful!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FYi - and for fun.  Here's the same workbook, where I added an additional Column to your source data, called Amount Owed.

I then Created a Pivot table on those 3 columns, and then created a filter from the Row values Customer ID, on Amount Owed, >= 1000 to generate the same list, using a pivot table.


buutercup0122Author Commented:
Thank you so much, Dave. I really appreciate it. I especially liked that you included your explanations. Also, thank you for the extra explanation of a pivot table! I am truly amazed with your knowledge.
Thanks again,
buutercup0122Author Commented:
Perfect. Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.