How to assign the Value of Yesterday's Date to a String Variable in Visual Basic For Applications

Posted on 2008-11-11
Last Modified: 2012-05-05
This question is related a little related to a previous question in which I had asked how to get yesterdays date typed into a word document in response to the click event of a button on a VBA form.
The answer was
Selection.TypeText Format(Now - 1, "dd MMMM YYYY")
and this did work.
I would now like to be able to assign the value of yesterday's date to a string variable and would appreciate help for that.

So that if I say
Dim YesterdayDate As String
YesterdayDate = ?
( I need help what should come in place of ? so that the String Variable YesterdayDate becomes equal to Monday, 10 November 2008 assuming that today's date is Tuesday, 11 November 2008))
Thank you for your help in anticipation.
Question by:FaheemAhmadGul
    LVL 17

    Assisted Solution

    How about
    Yesterday = Format(Now - 1, "dd MMMM YYYY")
    LVL 13

    Expert Comment

    Same concept as yestrerdaysdate = Date() -1

    Author Comment

    Many thanks for the prompt response to my query. Both of the above solutions work. The one by JohnGerhardt shows date as 10 November 2008 and one by leegclystvale shows date as 10/11/2008.
    Can any of the above solutions be modified a little so that the date also includes day of the week. So that the variable  YesterdaysDate becomes equal to Monday, 10 November 2008.
    LVL 13

    Expert Comment

    yesterdaysdate = Format(Now()-1, "Long Date")
    LVL 13

    Accepted Solution

    In fact ignore that, use yesterdaysdate = Format(now -1, "dddd, MMM d yyyy")

    Author Closing Comment

    Excellent! This worked perfectly. Many thanks. As this required some extra work and both of you contributed to the solution, I have increased the total points to 250 and split them 150+100. I hope this is OK.
    Once again thanking you both. Regards Faheem

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now