Solved

Infinite loop

Posted on 2013-12-25
10
384 Views
Last Modified: 2013-12-25
Can someone please point out why this code gets stuck of an infinite loop.

Demo(31)

Sub Demo(intDay As Integer)
    Dim strTmpDate As String
    Dim i As Integer

    For i = 1 To 6
        strTmpDate = Format(intDay & "/" & DatePart("M", Date + 30 * i) & "/" & DatePart("YYYY", Date + 30 * i), "DD/MM/YYYY")
        Do While Not IsDate(strTmpDate)
            strTmpDate = Format(intDay - 1 & "/" & DatePart("M", Date + 30 * i) & "/" & DatePart("YYYY", Date + 30 * i), "DD/MM/YYYY")
            Debug.Print strTmpDate
        Loop
    Next
End Sub

Open in new window

0
Comment
Question by:MacroShadow
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39739372
My guess would be that strTmpDate does not contain a valid date. Have you tried stepping through the code?
0
 
LVL 27

Author Comment

by:MacroShadow
ID: 39739376
Yes I have, the problem seems to be that although the code is supposed to subtract 1 day for every loop, it only does it once.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39739378
The variable strTmpDate is never changed in the inner while loop. Either you should alter the value of this variable for the checks and/or add an exit condition for the inner loop.
0
 
LVL 13

Assisted Solution

by:Alexander Eßer [Alex140181]
Alexander Eßer [Alex140181] earned 200 total points
ID: 39739381
Yes I have, the problem seems to be that although the code is supposed to subtract 1 day for every loop, it only does it once.
Then you should add a new variable which holds the calculated day(s) to subtract and put something like "varDay = varDay - 1" in your inner loop...
0
 
LVL 27

Author Comment

by:MacroShadow
ID: 39739384
Doesn't this line
strTmpDate = Format(intDay - 1 & "/" & DatePart("M", Date + 30 * i) & "/" & DatePart("YYYY", Date + 30 * i), "DD/MM/YYYY")

Open in new window

change the strTmpDate variable? That's what I intended it to do.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 81

Accepted Solution

by:
byundt earned 300 total points
ID: 39739390
Alex140181 is correct. You are subtracting 1 from a constant value. The following is working:
Sub Demo(intDay As Integer)
    Dim strTmpDate As String
    Dim i As Integer, j As Integer

    For i = 1 To 6
        strTmpDate = Format(intDay & "/" & DatePart("M", Date + 30 * i) & "/" & DatePart("YYYY", Date + 30 * i), "DD/MM/YYYY")
        j = 1
        Do While Not IsDate(strTmpDate)
            strTmpDate = Format(intDay - j & "/" & DatePart("M", Date + 30 * i) & "/" & DatePart("YYYY", Date + 30 * i), "DD/MM/YYYY")
            Debug.Print strTmpDate
            j = j + 1
        Loop
    Next
End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39739391
What does the "Debug.Print strTmpDate" show?! Is it always the same value?!
0
 
LVL 81

Expert Comment

by:byundt
ID: 39739393
I predict that you will get unexpected results, however, when the current day of the month is 12 or less. This is because VBA assumes date strings are mm/dd/yyyy unless the mm part is bigger than 12.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39739394
That's what I intended to say, byundt, but he should have gotten to that solution by himself ;-)
0
 
LVL 81

Expert Comment

by:byundt
ID: 39739400
If you want to return the last day of the month for 6 consecutive months, then consider:
Sub LastOfMonths()
Dim dat As Date
Dim i As Integer
dat = Date
For i = 1 To 6
    dat = DateSerial(Year(dat), Month(dat) + 2, 0)
    Debug.Print Format(dat, "DD/MM/YYYY")
Next
End Sub

Open in new window

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now