Solved

Help with Procedure to delete rows

Posted on 2014-07-24
7
130 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
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

770 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