Solved

Excel VBA minus one month

Posted on 2010-08-22
3
644 Views
Last Modified: 2012-05-10
Hi Experts

I have this macro which adds a MS Outlook appointment based on the selected Excel Cell (which is a date).  It works OK.

As you will see, the following line sets the appointment start to be at 8am, 31 days before the ActiveCell date.  

        .Start = ActiveCell.Value - 31 + TimeValue("08:00:00")

My question is, how can I change that to be ONE MONTH before the ActiveCell date rather than 31 days?  That way, if the ActiveCell date is 24/3/2011, .Start will be 24/2/2011.

Thanks

Will

Sub Set_Outlook_Reminder()
Dim objOutlook As Object
Dim objAppt As Object
Dim objNamespace As Object
Dim objFolder As Object
 
Worksheets("Customer Database").Activate
 
    Set objOutlook = CreateObject("Outlook.Application")
    
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set objFolder = objNamespace.GetDefaultFolder(9)
    Set objAppt = objFolder.Items.Add 'create task item
    With objAppt
        .Start = ActiveCell.Value - 31 + TimeValue("08:00:00")
        .End = .Start + TimeValue("00:30:00")
        .Subject = "Invoice " + ActiveCell.Offset(-2, 0).Value
        .Location = ""
        .Body = ""
        .BusyStatus = olBusy
        .ReminderMinutesBeforeStart = 120
        .ReminderSet = True
        .Save
    End With

Set objAppt = Nothing
Set objFolder = Nothing
Set objNamespace = Nothing
Set objOutlook = Nothing

MsgBox "Successfully Added to Outlook"

End Sub

Open in new window

0
Comment
Question by:willnjen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
ploftin earned 500 total points
ID: 33497232
To get the date of "one month ago", use this:
Sub Set_Outlook_Reminder()
Dim objOutlook As Object
Dim objAppt As Object
Dim objNamespace As Object
Dim objFolder As Object
 
Worksheets("Customer Database").Activate
 
    Set objOutlook = CreateObject("Outlook.Application")
    
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set objFolder = objNamespace.GetDefaultFolder(9)
    Set objAppt = objFolder.Items.Add 'create task item
    With objAppt
        .Start = DateAdd("m",1,ActiveCell.Value) + TimeValue("08:00:00")
        .End = .Start + TimeValue("00:30:00")
        .Subject = "Invoice " + ActiveCell.Offset(-2, 0).Value
        .Location = ""
        .Body = ""
        .BusyStatus = olBusy
        .ReminderMinutesBeforeStart = 120
        .ReminderSet = True
        .Save
    End With

Set objAppt = Nothing
Set objFolder = Nothing
Set objNamespace = Nothing
Set objOutlook = Nothing

MsgBox "Successfully Added to Outlook"

End Sub

Open in new window

0
 

Author Comment

by:willnjen
ID: 33497293
Perfect, except it added one month so i changed it to the following...

        .Start = DateAdd("m",-1,ActiveCell.Value) + TimeValue("08:00:00")

Thanks for your help!!!
0
 
LVL 5

Expert Comment

by:ploftin
ID: 33497374
Sorry about that. My math is off today lol. :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question