Shopping & Style0 min ago
Dealing with clock changes in MS Excel.
4 Answers
I'd like to get Excel to do a calculation only if the date is the last Sunday in October (for the extra hour in that day).
I'd like this to be coded rather than me having to identify the last Sunday in October for each year.
Eventually I'd also like to omit the calculation for the 24th hour of the last Sunday in March.
Any suggestions for an elegant way of doing it? I was thinking of using the IF function, but not sure how I would code "the last Sunday in October" yet.
I'd like this to be coded rather than me having to identify the last Sunday in October for each year.
Eventually I'd also like to omit the calculation for the 24th hour of the last Sunday in March.
Any suggestions for an elegant way of doing it? I was thinking of using the IF function, but not sure how I would code "the last Sunday in October" yet.
Answers
Best Answer
No best answer has yet been selected by tell-me-more. Once a best answer has been selected, it will be shown here.
For more on marking an answer as the "Best Answer", please visit our FAQ.Public Function Calc_Date() As Boolean
Dim LValue As Integer
myDay = 24
myMonth = 10
myYear = Year(Now())
Do
myDay = myDay + 1
LValue = DatePart("w", myDay & "/" & myMonth & "/" & myYear, vbMonday, vbFirstJan1)
Loop Until LValue = 7
If Trim$(Date) = myDay & "/" & myMonth & "/" & myYear Then
Calc_Date = True
Else
Calc_Date = False
End If
End Function
Public Function Calc_Hour() As Boolean
Dim LValue As Integer
myDay = 24
myMonth = 3
myYear = Year(Now())
If Len(myMonth) = 1 Then myMonth = "0" & myMonth
Do
myDay = myDay + 1
LValue = DatePart("w", myDay & "/" & myMonth & "/" & myYear, vbMonday, vbFirstJan1)
Loop Until LValue = 7
myHour = DatePart("h", myDay & "/" & myMonth & "/" & myYear, vbMonday, vbFirstJan1)
If Trim$(Date) = myDay & "/" & myMonth & "/" & myYear And myHour = 0 Then
Calc_Hour = True
Else
Calc_Hour = False
End If
End Function
Dim LValue As Integer
myDay = 24
myMonth = 10
myYear = Year(Now())
Do
myDay = myDay + 1
LValue = DatePart("w", myDay & "/" & myMonth & "/" & myYear, vbMonday, vbFirstJan1)
Loop Until LValue = 7
If Trim$(Date) = myDay & "/" & myMonth & "/" & myYear Then
Calc_Date = True
Else
Calc_Date = False
End If
End Function
Public Function Calc_Hour() As Boolean
Dim LValue As Integer
myDay = 24
myMonth = 3
myYear = Year(Now())
If Len(myMonth) = 1 Then myMonth = "0" & myMonth
Do
myDay = myDay + 1
LValue = DatePart("w", myDay & "/" & myMonth & "/" & myYear, vbMonday, vbFirstJan1)
Loop Until LValue = 7
myHour = DatePart("h", myDay & "/" & myMonth & "/" & myYear, vbMonday, vbFirstJan1)
If Trim$(Date) = myDay & "/" & myMonth & "/" & myYear And myHour = 0 Then
Calc_Hour = True
Else
Calc_Hour = False
End If
End Function
^^^^^^^^^^^^^^^^^^^^^^
If you paste the above into an Excel VBA Module you will see two functions...
Calc_Date will return TRUE if the date today is the last Sunday in October, automatically worked out..
Calc_Hour will return TRUE if the hour now is 0 (or past midnight) and the current date is the last Sunday in March... again calc'ed automatically...
This code was made from scratch and not ripped from a site.. so if you use it, a thanks wouldnt go amiss.
If you paste the above into an Excel VBA Module you will see two functions...
Calc_Date will return TRUE if the date today is the last Sunday in October, automatically worked out..
Calc_Hour will return TRUE if the hour now is 0 (or past midnight) and the current date is the last Sunday in March... again calc'ed automatically...
This code was made from scratch and not ripped from a site.. so if you use it, a thanks wouldnt go amiss.
Related Questions
Sorry, we can't find any related questions. Try using the search bar at the top of the page to search for some keywords, or choose a topic and submit your own question.