Solved

DO Loop not working

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

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 31

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
A WS within a WS 11 40
Cross checking component service dlls 2 41
Populate Excel combo box with dependent vlookup 5 49
VB.NET Parsing UDP Bytes 15 35
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

739 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