Solved

Help with Procedure to delete rows

Posted on 2014-07-24
7
138 Views
Last Modified: 2014-07-26
Attached s-sheet has a date in it (D6) that I am trying to run an if < Now() on and it is always coming back false when it is true?  Using a VBA command, how do I reformat that cell so it is a date?  and returns a True?

Actually what I am trying to do is kill every row until the next date in column D if < now().  When the next date is reached check that date to and do it again if true and still < now() , until a false value is reached.

Another issue I am coming across is the empty cell under the date have spaces in it, so I cannot use while(not(isempty...  how can I delete the rows when they are not really empty or blank?

Could I get some help writing this in VBA?  Thanks.
DateLessThanToday.xlsx
0
Comment
Question by:RWayneH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:gowflow
ID: 40217025
Hi is this what you want ?

In this file I made up some more data and copied sheet1 into sheet1 (2)
position yourself in either one of the 2 sheets and in the developer menu activate the macro DeleteRow and check the results you can compare with the result in the copied sheet.

here is the code

Sub DelRows()
Dim WS As Worksheet
Dim MaxRow As Long, I As Long
Dim LastDate As Date, ThisDate As Date
Set WS = ActiveSheet
MaxRow = LastRow
I = 1

Do
    If Len(WS.Cells(I, "D")) = 20 And Mid(WS.Cells(I, "D"), 3, 1) = "/" Then
        ThisDate = DateValue(Left(WS.Cells(I, "D"), 10))
    End If
    
    If ThisDate <> LastDate And ThisDate < DateValue(Now) Then
        WS.Cells(I, "A").EntireRow.Delete
        MaxRow = MaxRow - 1
    Else
        I = I + 1
    End If
    
Loop Until I > MaxRow

MsgBox ("All date prior to " & DateValue(Now) & " have been deleted with their coresponding rows successfully.")

End Sub


Function LastRow() As Long
Dim ExcelLastCell As Object
ActiveSheet.DisplayPageBreaks = False
Set ExcelLastCell = ActiveSheet.Cells.SpecialCells(xlLastCell)
LastRow = ExcelLastCell.Row

End Function

Open in new window



gowflow
DateLessThanToday.xlsm
0
 

Author Comment

by:RWayneH
ID: 40217089
Hmm.  Looks ok.  How would we get the header information to stay and start deletes from cell D6?  Still testing.  Also could you comment some of the code?  Would like to follow what it is doing while stepping thru procedure.  Thanks.
0
 
LVL 30

Accepted Solution

by:
gowflow earned 500 total points
ID: 40217118
ok here it is. Code commented and start row 6

gowflow
DateLessThanToday-V01.xlsm
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:RWayneH
ID: 40218978
I've requested that this question be closed as follows:

Accepted answer: 0 points for RWayneH's comment #a40217089

for the following reason:

EXCELent!!  Worked great. Thanks.
0
 
LVL 30

Expert Comment

by:gowflow
ID: 40218979
Hi
I think you closed this question by mistake attributing 0 points to yourself although you ackgnowledged that the solution I provided was GREAT. For this reason I stoped the autoclosure to allow you to close it properly.

Regards
gowflow
0
 

Author Closing Comment

by:RWayneH
ID: 40221565
This solution is working. Thanks.
0
 
LVL 30

Expert Comment

by:gowflow
ID: 40221612
your welcome.
gowflow
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

728 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