?
Solved

DO Loop not working

Posted on 2016-08-12
4
Medium Priority
?
97 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 36

Accepted Solution

by:
Kimputer earned 2000 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 32

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 36

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 32

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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 will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

762 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