View Single Post
 
Old 07-22-2017, 12:47 PM
gmaxey gmaxey is offline Windows 7 32bit 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