Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-21-2017, 04:42 AM
StephenRay StephenRay is offline Fifteen Days From Today's Date, (Not Counting Saturday & Sunday). Windows 7 64bit Fifteen Days From Today's Date, (Not Counting Saturday & Sunday). Office 2010 64bit
Advanced Beginner
Fifteen Days From Today's Date, (Not Counting Saturday & Sunday).
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 53
StephenRay is on a distinguished road
Default Fifteen Days From Today's Date, (Not Counting Saturday & Sunday).

I am just starting to learn VBA. I have a good book, and I search the 'net for examples to go by.


Ultimately, I want a Macro to add fifteen week-days to the current date and display the date in this format: 07-21-2017. -Not including Saturday and Sunday.
But I am stuck with a compile error. It says "Object Required" and it highlights:
Set iCounter = 0

All the examples I have seen make the counter an integer, and I have studied data objects. But can you make an object an integer?

Here's what I have written so far:

Sub Macro4()
'
' Macro4 Macro
'
'

Dim fifteenDaysFromNow As Date
Dim iCounter As Integer
'This next line is highlighted with the compile error
Set iCounter = 0
Dim day As DayOfWeek
Set DayOfWeek = currentDateTime.DayOfWeek

Do
fifteenDaysFromNow = DateAdd("d", 1, Date)
If day = DayOfWeek.Saturday Or day = DayOfWeek.Sunday Then
iCounter = -1
Else
iCounter = +1
Loop While iCounter <= 16
End If

End Sub
Reply With Quote
  #2  
Old 07-21-2017, 04:53 AM
gmayor's Avatar
gmayor gmayor is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 10 Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 of
Default

I think you have made up some of your own VBA syntax there The immediate problem you have mentioned is the use of Set
The line should be simply iCounter = 0, but then you will find other problems. Use the following function instead, which will add weekdays to a given date - here Today, so add 15 days to that and you get 11th August. The first macro shows how to call the function.

Code:
Sub Macro1()
Selection.TypeText Format(DateDue(Date, 15), "mm-dd-yyyy")
End Sub

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

    iDays = iDays + iCount
    vDate = DateAdd("d", iCount * -1, vDate)
    DateDue = DateAdd("d", iDays Mod 5, DateAdd("ww", Int(iDays / 5), vDate))
lbl_Exit:
    Exit Function
End Function
__________________
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
  #3  
Old 07-21-2017, 05:46 AM
StephenRay StephenRay is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 64bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2010 64bit
Advanced Beginner
Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday).
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 53
StephenRay is on a distinguished road
Default

gmayor, Thanks for your reply. Yes, indeed, I know my syntax skills are not skilled yet. The syntax may be from Visual Basic instead of VBA, (If there a difference?). I have good books for both Visual Basic and VBA. I get code from many different sources.
I will study your Macro and function.
And I need to study Functions too.
I will go work some more and get back to you if/when I get stuck.
Reply With Quote
  #4  
Old 07-21-2017, 07:01 AM
StephenRay StephenRay is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 64bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2010 64bit
Advanced Beginner
Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday).
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 53
StephenRay is on a distinguished road
Default

Gmayor, Your code works!
Now I know how a function works. And you wrote it quick in just a few minutes! I like a man who knows his onions.
I am going to study this!
Later I will make it display using msg.
But don't tell me how. I will learn by struggling with it.
Reply With Quote
  #5  
Old 07-21-2017, 11:03 AM
StephenRay StephenRay is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 64bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2010 64bit
Advanced Beginner
Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday).
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 53
StephenRay is on a distinguished road
Default

Gmayor,
I added to your code to highlight and copy the date.
It also displays a message in the Status Bar.
Here it is:
Sub Macro2()
'
'Sub Macro1()
Dim strBothItems As String
Dim firstPart As String
Dim datePart As String
firstPart = "Fifteen Days from Today "
datePart = Format(DateDue(Date, 15), "mm-dd-yyyy")
strBothItems = firstPart & datePart
Selection.TypeText Format(DateDue(Date, 15), "mm-dd-yyyy")
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Application.StatusBar = strBothItems
‘Application.StatusBar = Format(DateDue(Date, 15), "mm-dd-yyyy")
'Application.StatusBar = "Fifteen Days from Today "
End Sub

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

iDays = iDays + iCount
vDate = DateAdd("d", iCount * -1, vDate)
DateDue = DateAdd("d", iDays Mod 5, DateAdd("ww", Int(iDays / 5), vDate))
lbl_Exit:
Exit Function
End Function
Reply With Quote
  #6  
Old 07-22-2017, 12:47 PM
gmaxey gmaxey is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 32bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
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
  #7  
Old 07-22-2017, 08:24 PM
gmayor's Avatar
gmayor gmayor is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 10 Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 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
  #8  
Old 07-23-2017, 03:06 PM
macropod's Avatar
macropod macropod is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 64bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You can do all of this without a macro! To see how to do this and just about everything else you might want to do with dates in Word, check out my Microsoft Word Date Calculation Tutorial:
https://www.msofficeforums.com/word/...-tutorial.html
In particular, look at the item titled Handling Weekends and Holidays in Calculated Dates. Do read the document's introductory material.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 07-24-2017, 06:00 AM
StephenRay StephenRay is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 64bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2010 64bit
Advanced Beginner
Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday).
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 53
StephenRay is on a distinguished road
Default

Gentlemen, Thank you all for your interest. All of you are way ahead of me. This code certainly is elegant. I have been studying it all weekend.
I used to code elegant stuff like this in COBOL that nobody else could understand.
And right now, I am studying this line in the code:
DateDue = DateAdd("d", iDays Mod 5, DateAdd("ww", Int(iDays / 5), vDate))

This is very compact and elegant!
It will take me a few days to understand it because I have to study the basics first, and frankly, while this is really fun and really useful to me personally, I have many other responsibilities.

All of your words seem like gold to me. Thank you!

And yes, later I need to be able to add about ten different holidays, some of which change yearly. Christmas is always December 25, but our United States Thanksgiving Holiday is never on the same date. So I would need to be able to change the holiday dates.

Last edited by StephenRay; 07-24-2017 at 06:06 AM. Reason: One more comment
Reply With Quote
  #10  
Old 07-25-2017, 10:07 PM
macropod's Avatar
macropod macropod is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 64bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

FWIW, the field code solution I directed you to shows how to accommodate multiple holidays whose dates are calculated in a variety of ways, including:
• preset holidays, whose date is determined by their day number in a normal year;
• the Nth instance of a day of the week in a given month; and
• Easter,
and to substitute either the preceding or following weekday, as appropriate.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 07-26-2017, 04:55 AM
StephenRay StephenRay is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 64bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2010 64bit
Advanced Beginner
Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday).
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 53
StephenRay is on a distinguished road
Default

Macropod, Yes, I haven't had a chance to look at it yet, but I will. I want to get the holidays working with this. There are about ten of us here that administer Microsoft Active Directory and other employee documents. When I get it going I will share it with everybody. Thanks!
Reply With Quote
  #12  
Old 07-26-2017, 05:08 AM
StephenRay StephenRay is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 64bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2010 64bit
Advanced Beginner
Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday).
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 53
StephenRay is on a distinguished road
Default

gmaxey, Thank you for your suggestion to study Range. I have lightly studied it but will study more tonight. Range is a method, not an object, my book says. I thought your oRng was a Key Word, and I couldn't find it anywhere! Ha! But now I know your Camel Notation shows it is a variable. And my book says to explicitly declare variables. So you see the trouble I have at this early stage.
From my experience with COBOL, I used to take hours studying some point or concept or piece of syntax that yielded big benefits later. Few people understood my code but it was compact and sophisticated and worked well.
Reply With Quote
  #13  
Old 07-26-2017, 05:27 AM
Cosmo Cosmo is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows Vista Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

BTW, not necessarily the solution you're looking for, but in this case all you have to do is add 21 days to the date (3 weeks), as this will always be 15 week days away (3 5-day weeks).

Doesn't work if you are looking for a date range that is not divisible by 5. Or if you need to accommodate holidays (which the original post doesn't mention as criteria)
Reply With Quote
  #14  
Old 07-26-2017, 11:22 AM
gmaxey gmaxey is offline Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Windows 7 32bit Fifteen Days From Today's Date, (Not Counting Saturday &amp; Sunday). Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

SR,

Yes range is a method of Selection among other things but when declared as a variable, it is an object. Step through this code and watch your locals window.

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/26/2017
Dim oRng As Object
Dim oRng1 As Range
Dim oRng2
  Set oRng = Selection.Range
  Set oRng = Selection.Range
  Set oRng2 = Selection.Range
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Fifteen Days From Today's Date, (Not Counting Saturday &amp; 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 06:54 AM.


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