Solved

DO Loop not working

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

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 29

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

825 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