[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-24
4
Medium Priority
?
347 Views
Last Modified: 2012-05-12
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!!
Customer-Accounts.xlsx
0
Comment
Question by:buutercup0122
  • 2
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
dlmille earned 2000 total points
ID: 37022799
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
    
    outSht.Activate
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!

Dave
Customer-Accounts-r1.xlsm
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37022834
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.

Cheers,

Dave
Customer-Accounts-r2.xlsm
0
 

Author Comment

by:buutercup0122
ID: 37022900
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,
TC
0
 

Author Closing Comment

by:buutercup0122
ID: 37022905
Perfect. Thank you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question