Solved

DO Loop not working

Posted on 2016-08-12
4
72 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 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.
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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now