Solved

Help with Procedure to delete rows

Posted on 2014-07-24
7
126 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
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:gowflow
Comment Utility
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
Comment Utility
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
Comment Utility
ok here it is. Code commented and start row 6

gowflow
DateLessThanToday-V01.xlsm
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:RWayneH
Comment Utility
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
Comment Utility
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
Comment Utility
This solution is working. Thanks.
0
 
LVL 29

Expert Comment

by:gowflow
Comment Utility
your welcome.
gowflow
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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 will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now