?
Solved

Looping is not working using For - Next Code

Posted on 2012-09-21
3
Medium Priority
?
532 Views
Last Modified: 2012-09-21
I have a macro using For & Next. The code works if the code is run multiple times. I would like the code to complete the process by pressing the macro button once.

Can someone look at the code and make the necessary changes PLEASE.

This macro deletes all dates in column "B" if less than column "F" less another 5 days.

Sub DeleteRows_Dates()

Dim x As Long
Dim LastRow As Integer

Range("B3").Select
LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row

    For x = 3 To LastRow
     Debug.Print Cells(x, "B").Value
        If CDate(Cells(x, "B")) < CDate(Cells(x, "F") - Day(5)) Then
            Cells(x, "B").EntireRow.Delete
        End If
    Next x
   
End Sub
Deletes-Date2.xlsm
0
Comment
Question by:user2073
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Martin Liss earned 2000 total points
ID: 38423819
You should run it backwards because deleting rows affects the For/Next index.

Sub DeleteRows_Dates()

Dim x As Long
Dim LastRow As Integer

Range("B3").Select
LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row

    For x = LastRow to 3 Step -1
     Debug.Print Cells(x, "B").Value
        If CDate(Cells(x, "B")) < CDate(Cells(x, "F") - Day(5)) Then
            Cells(x, "B").EntireRow.Delete
        End If
    Next x
    
End Sub

Open in new window


And just in case you might ever have more than 32767 rows, you should make LastRow a Long.
0
 

Author Closing Comment

by:user2073
ID: 38423829
Wow, you guys are quick. Yes the changes you make work THANKS.
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38423861
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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 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.

862 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