Friday, October 14, 2016

VBA Macro to find next workday after variable number of days considering old indian style week off(alternate saturday off and fixed sunday off),VBA Teacher Sourav,Kolkata 09748184075


Sub test()
Sheets(1).Select

Dim exampleDate As Date
Dim initiandate As Date
Dim enddate As Date

exampleDate = DateValue("10/5/2016")
initialdate = DateValue("10/5/2016")
'exampleDate = exampleDate + 1
'MsgBox (Day(exampleDate))
'MsgBox WeekdayName(3, True, vbMonday)
'Dim val As String
' Range("B1").Select
 '   ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""ddd"")"
  '  val = ActiveCell.Value
 ' MsgBox WeekdayName(Weekday(exampleDate), True, vbSunday)
Dim result As Integer
result = 0
Dim i, j As Integer
j = 21
i = 1
Dim check As Integer
check = 0

While i <= j
If WeekdayName(Weekday(exampleDate), True, vbSunday) = "Sun" Or WeekdayName(Weekday(exampleDate), True, vbSunday) = "Sat" Then
'MsgBox ("Sun")
check = 1
j = j + 1
i = i + 1
GoTo lastline
End If
exampleDate = exampleDate + 1
i = i + 1
'MsgBox (result)
lastline:
If check = 1 Then
exampleDate = exampleDate + 1
check = 0
End If

'MsgBox (exampleDate)
Wend

'MsgBox (exampleDate)
'MsgBox (exampleDate)
enddate = exampleDate

Dim sampledate As Date

Dim sampledatestr As String
sampledatestr = (CStr(Month(initialdate)) + "/" + CStr(1) + "/" + CStr(Year(initialdate)))
'MsgBox (sampledatestr)
sampledate = DateValue(sampledatestr)
Dim tempdate As Date
tempdate = sampledate
'MsgBox (tempdate)
'MsgBox (enddate)
'MsgBox (sampledate)
check = 0
For sampledate = tempdate To enddate
If WeekdayName(Weekday(sampledate), True, vbSunday) = "Sat" Then
If check = 1 Then
exampleDate = exampleDate - 1
check = 0

End If



End If
check = 1

Next sampledate

MsgBox (exampleDate)



End Sub

No comments:

Post a Comment