?
Solved

Help with Procedure to delete rows

Posted on 2014-07-24
7
Medium Priority
?
141 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 31

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 31

Accepted Solution

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

gowflow
DateLessThanToday-V01.xlsm
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 31

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 31

Expert Comment

by:gowflow
ID: 40221612
your welcome.
gowflow
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

752 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