Solved

DO Loop not working

Posted on 2016-08-12
4
64 Views
Last Modified: 2016-08-12
Hi, can someone find out why this macro is not working? It has a DO Loop which should delete rows where the Start Date is today or later
CD-Weekly-Analysis_11AUG2016.xlsm
0
Comment
Question by:JCutcliffe
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 41753558
Try this instead

ElseIf CDate(Cells(i, 14).Value) >= CDate(Date) Then

Open in new window


1. Startdate was col N, which is 14.
2. Better not compare dates as string, it's unpredictable.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41753579
Since you are deleting the rows, you have to loop in reverse order to delete all the relevant rows...

You may use the following code also.....

Sub change()
Dim lr As Long, i As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 1 Step -1
   If Cells(i, 2) <> "" And Cells(i, 2) = "2728007" Then
      Cells(i, 19).Value = "35000000"
   ElseIf Cells(i, 14).Value > Date - 1 Then
      Rows(i).Delete
   End If
Next i
End Sub

Open in new window

0
 
LVL 35

Expert Comment

by:Kimputer
ID: 41753585
Yes, please implement the part of the second answer as well (from bottom to top), I missed that (don't copy the elseif part though, it doesn't include the CDATE I mentioned)
The reason being, if you go from top to bottom, you might miss a few steps (if you delete a row, the row that comes in its place will not be taken by the loop unless you bring the counter one back).
So either implement botton to top, or when deleting a row, counter = counter -1
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41753587
You will only need to change the following line if you don't want to loop until the header row. So the line 4 should be like this....

For i = lr To 2 Step -1

Open in new window


All other lines have no issues, they will do the job for you.
0

Featured Post

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).

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

747 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

11 Experts available now in Live!

Get 1:1 Help Now