A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » Worksheet Functions
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Conditional Delete Statement



 
 
Thread Tools Display Modes
  #1  
Old September 9th, 2010, 09:22 PM
chrisjack001 chrisjack001 is offline
Member
 
First recorded activity by OfficeFrustration: Sep 2010
Posts: 1
Default Conditional Delete Statement

I want to create a code that can run through the entire spreadsheet and delete the rows of all accounts that have $0 or less Outstanding balance. Example in the above sample account 31203 started with an outstanding balance of $21,803 but had one transaction that resulted in an ending balance of $0. As a result of this all rows of account 31203 should be deleted and data moved up. All accounts that have Outstanding balances like account # 76292 & 56512 in the above will not be deleted because they have outstanding balances of $1000 and $24.900 respectively. The only Accounts that will remain on the spreadsheet will be account numbers with Outstanding balances. Can you please help me create a macro or a VBA code to accomplish this task I am new at this. Thanks for your help.
Ads
  #2  
Old March 15th, 2011, 06:55 PM
tarquinious tarquinious is offline
Experienced Member
 
First recorded activity by OfficeFrustration: Mar 2011
Posts: 34
Default

Quote:
Originally Posted by chrisjack001 View Post
I want to create a code that can run through the entire spreadsheet and delete the rows of all accounts that have $0 or less Outstanding balance. Example in the above sample account 31203 started with an outstanding balance of $21,803 but had one transaction that resulted in an ending balance of $0. As a result of this all rows of account 31203 should be deleted and data moved up. All accounts that have Outstanding balances like account # 76292 & 56512 in the above will not be deleted because they have outstanding balances of $1000 and $24.900 respectively. The only Accounts that will remain on the spreadsheet will be account numbers with Outstanding balances. Can you please help me create a macro or a VBA code to accomplish this task I am new at this. Thanks for your help.
Hi there ChrisJack001,

I have had a stab at writing a macro for you to perform this function. The macro below relies on the account numbers being sorted (or at least grouped together) and starting in cell A1, whilst the corresponding values are in the column next to them (i.e. column B). If this isn't the case, amend the code, or let me know and I can make appropriate changes for you.

Also, if you need detailed instructions how to put this macro in, and run it, let me know.

Code:
Sub DeleteSettled()
    Dim CurrentRow As Integer
    Dim CurrentAccount, RowsToDelete As Variant
    Range("A1").Activate ' This is the position of the first account number - change to suit
    Do Until ActiveCell = "" ' Loop until there are no more account numbers
        CurrentRow = ActiveCell.Row ' Holds the top row of a group of matching accounts
        CurrentAccount = ActiveCell.Value
        Do Until ActiveCell.Offset(1, 0)  CurrentAccount ' Move down to the last entry for this account
            ActiveCell.Offset(1, 0).Activate
        Loop
        If ActiveCell.Offset(0, 1) = 0 Then ' Final balance=$0?
            RowsToDelete = CurrentRow & ":" & ActiveCell.Row 'Build the string for rows to delete
            Rows(RowsToDelete).Select ' Delete the rows
            Selection.Delete Shift:=xlUp
        Else
            ActiveCell.Offset(1, 0).Activate 'Move to the next account
        End If
    Loop
End Sub
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:19 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright 2004-2018 OfficeFrustration.
The comments are property of their posters.