Solved

Help with Procedure to delete rows

Posted on 2014-07-24
7
136 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 29

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 29

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 29

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 29

Expert Comment

by:gowflow
ID: 40221612
your welcome.
gowflow
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

738 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