Quantcast
Viewing all articles
Browse latest Browse all 20

Looping Problem

 C McHale wrote:

Thanks Guys,

 

John: In your first reply you show how to skip weekends and run for 14 days total.  What do i need to do to skip weekends and run for say 30 weekdays.  Please also keep in mind that when these calculations are running, they may possibly be calculating data for upto 6 years.

 

Thanks

 

Hi,

 

30 weekdays is 6 weeks so change the 14 to 42 at the bottom of the Do Loop for 6 whole weeks.

It won't matter if the day it starts on isn't a Monday, you will still get 30 weekdays of calculations.

 

You may want to show a progress bar to the end user to show something is happening if you are going to run calculations for up to 6 years.

 

 

 

 

Regards,

 

John

 

P.S. Please mark my post(s) As Answer if they have answered your questions.

 

 

 

Code Block

 

PublicClass Form1

 

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

'Set a DATE.

Dim myDate AsDate = Now

Dim loopCount_NumberOfDays AsInteger = 0

Dim outputString AsString = ""

Do

'To skip a specific holiday like Christmas Day and New Years day.>>

If myDate.Month = 12 And myDate.Day = 25 Then

myDate = myDate.AddDays(1)

loopCount_NumberOfDays += 1

outputString &=

"**** Christmas Day skipped!! ****"& vbCrLf

EndIf

If myDate.Month = 1 And myDate.Day = 1 Then

myDate = myDate.AddDays(1)

loopCount_NumberOfDays += 1

outputString &=

"**** New Years Day skipped!! ****"& vbCrLf

EndIf

 

SelectCase myDate.DayOfWeek

Case DayOfWeek.Monday To DayOfWeek.Friday

outputString &= myDate.ToShortDateString & vbCrLf

'Do your calculations here.>>

 

 

 

 

'Add one to the current date.

myDate = myDate.AddDays(1)

loopCount_NumberOfDays += 1

Case DayOfWeek.Saturday

'Add 1 day if it is a Saturday to show the Sunday message!!

myDate = myDate.AddDays(1)

loopCount_NumberOfDays += 1

outputString &=

"**** Saturday skipped!! ****"& vbCrLf

Case DayOfWeek.Sunday

'Add 1 day if it is a Sunday.

myDate = myDate.AddDays(1)

loopCount_NumberOfDays += 1

outputString &=

"**** Sunday skipped!! ****"& vbCrLf

EndSelect

'Loop for 42 days in total.

LoopUntil loopCount_NumberOfDays = 42

MessageBox.Show(outputString)

MessageBox.Show(

"Loop ran for "& loopCount_NumberOfDays.ToString & " days.")

EndSub

End

Class

 

 

 

 

 

 

 


Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>