Consulting

Results 1 to 5 of 5

Thread: Solved: Checking if macro run again- same month

  1. #1
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location

    Solved: Checking if macro run again- same month

    Hello, can anyone help?

    This is working correctly however if the macro is run today, for the first time, it should NOT come up with the Warning message, at present with this code it does.
    The Warning SHOULD come up if run again on the same day or same month of the same year later on.
    [VBA]
    Sub DateStamp()
    Dim rowsT

    Sheets("Page").Activate
    Range("A2").Select
    Selection.Value = Now()
    rowsT = Selection.Value

    If Format(rowsT, "mmm") >= Format(Date, "mmm") Then
    MsgBox "Warnng”
    End if

    End sub
    [/VBA]

  2. #2
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    Your current macro places Now() into the cell then checks to see if Now() is greater or equal to Now(). Except for the milliseconds it takes to run the macro, Now() will = Now().

    You also check the dates using format(date,"'mmm"). The value returned is the 3 character string of the month. Jan will be less than Feb, Mar, May, Jun, Jul, Sep, Oct and Nov, but greater than Apr, Aug, and Dec.

    Try this version:
    [VBA]Sub DateStamp()
    Dim rowsT, prev

    prev = Sheets("Page").Range("a2")
    rowsT = Now()

    If Month(rowsT) = Month(prev) And Year(rowsT) = Year(prev) Then
    MsgBox "Warnng” "
    Else
    Sheets("Page").Range("A2") = Now()
    End If

    End Sub[/VBA]

  3. #3
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location

    thanks

    thankyou this works. But just thought if they do want to bypass the warning I should allow them to do so. therefore I ammened this line

    [VBA] MsgBox "Warning, this macro has already been run this month, would you like to run again?” ", vbYesNo, "Warning Message!" [/VBA]

    Can you help me with the rest of the code from here.

  4. #4
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    The following will check for the date entry in A2. If the cell is blank, it enters the Now() value. If there is data in the cell, it continues as it had previously. It now contains the option to continue if the macro is run in the same month.

    [vba]Sub DateStamp2()
    Dim rowsT, prev, bContinue
    Dim keepGoing As Boolean, newDate As Boolean

    If Sheets("Page").Range("A2") = "" Then
    Sheets("Page").Range("A2") = Now()
    newDate = True
    Else
    prev = Sheets("Page").Range("a2")
    rowsT = Now()
    End If


    If Month(rowsT) = Month(prev) And Year(rowsT) = Year(prev) And newDate = False Then
    bContinue = MsgBox("Warning, this macro has already been run this month, would you like to run again?", _
    vbYesNo, "Warning Message!")
    If bContinue = vbYes Then
    keepGoing = True
    End If
    Else
    Sheets("Page").Range("A2") = Now()
    keepGoing = True
    End If
    If keepGoing = False Then Exit Sub

    'rest of code


    End Sub[/vba]

  5. #5
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    perfect thank you very much for your effort!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •