Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-22-2017, 12:47 PM
gmaxey gmaxey is offline Fifteen Days From Today's Date, (Not Counting Saturday & Sunday). Windows 7 32bit Fifteen Days From Today's Date, (Not Counting Saturday & Sunday). Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,636
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Couple of things.

1. As a new coder you have the same tendency as I had. You declare a lot of extraneous variables and you seem stuck on "Selection." Learn to use the range object and unless needed for clarity minimize variables.



Code:
Option Explicit
Sub Macro1()
Dim oRng As Range
  Set oRng = Selection.Range
  oRng.Text = Format(DateDue(Date, 15), "mm-dd-yyyy")
  'I assume  you want to copy this resulting date value to the clipboard for use later.
  'Otherwise this next line is not needed.
  oRng.Copy
  Application.StatusBar = "Fifteen Days from Today " & oRng.Text
End Sub
2. Discussed with Graham offline. His function actually returns the non-weekday after the span defined by iDays.

Code:
Public Function DateDue(ByVal vDate As Date, ByVal iDays As Variant) As Variant
'Graham Mayor - http://www.gmayor.com - Last updated - 14 May 2017
'Add weekdays to date - from Monday if date falls at weekend
Dim iCount As Integer
  DateDue = Null
  If IsNull(vDate) Or IsNull(iDays) Then
    GoTo lbl_Exit
  End If
  Select Case Weekday(vDate)
    Case Is = 1 ' Sunday
      vDate = DateAdd("d", 1, vDate)
      iCount = 0
   Case Is = 7 ' Saturday
      vDate = DateAdd("d", 2, vDate)
      iCount = 0
   Case Else
      iCount = Weekday(vDate) - 2
  End Select
  'Note, Graham's code actually returns the non-weekday 1 day after the span passed by iDays
  iDays = iDays + iCount
  'If you want the "iDay" use:
  'iDays = iDays + iCount - 1
  vDate = DateAdd("d", iCount * -1, vDate)
  DateDue = DateAdd("d", iDays Mod 5, DateAdd("ww", Int(iDays / 5), vDate))
lbl_Exit:
  Exit Function
End Function
What if your due date falls on Labor Day, Christmas, or some other holiday ;-)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #2  
Old 07-22-2017, 08:24 PM
gmayor's Avatar
gmayor gmayor is offline Fifteen Days From Today's Date, (Not Counting Saturday & Sunday). Windows 10 Fifteen Days From Today's Date, (Not Counting Saturday & Sunday). Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
Originally Posted by gmaxey View Post
What if your due date falls on Labor Day, Christmas, or some other holiday ;-)
For that you'll need a look-up table - http://www.gmayor.com/FutureDateAddIn.htm
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Fifteen Days From Today's Date, (Not Counting Saturday & Sunday). Counting the difference in days within multiple groups wheddingsjr Excel 2 03-28-2017 07:38 AM
Sum Function over Today +/- 60 Days gabeha Excel 2 09-12-2014 01:13 AM
Using Outlook Today Calendar Days cwksr Outlook 0 08-13-2014 10:57 AM
Conditional formatting of Today +21 days? SHERMAN Excel 3 12-20-2010 08:08 AM
Creating an Auto-Calc'd Date? Today+7 Days? SoCalTelephone Word 0 10-06-2010 10:27 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:40 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft