Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Visual Basic (https://www.askmehelpdesk.com/forumdisplay.php?f=469)
-   -   VBA code (https://www.askmehelpdesk.com/showthread.php?t=423188)

  • Dec 7, 2009, 11:14 PM
    rsdjimbie
    VBA code
    Once the month ends, cell "A1" must tick over to zero, where "A1" is used by me as a counter to show how much has been deliverd to date for the month.
  • Dec 8, 2009, 01:22 PM
    Perito

    Basically, you need to detect when the month rolls over.

    1. Unless the program is running all of the time (is it?), this can be tough to do. I'd suggest that whenever the program (Excel) terminates, you write a VBA routine that writes the current date and time to a disk file. If you open the program again, another VBA startup routine should look for the file. If it exists, the program can read the file and determine if the month has rolled over. If it has, it sets the appropriate cell to zero.

    2. While the program is running, you need to start an event that will trigger periodically. Excel has an "OnTime" function that you can use to do that. This is described in this article.

    Pearson Software Consulting

    The article also shows you how to use Windows Timers, if you wish to do that. For your application, you'll probably be best served using the OnTime function. I'd probably set it up to run once an hour, if I were you.

    In your subroutine -- the one that's invoked by the OnTime event, you'll have to determine the current month and compare it with the last month that you saved. You can do this as follows:

    Dim CurrentMonth as integer
    Dim NewMonth as integer

    Private Sub DetermineMonth
    NewMonth = Month(Now)
    If NewMonth <> CurrentMonth then
    Cells(Range("A1")) = 0 ' use this
    Cells(1,1) = 0 ' or this
    ' or any other way of assigning a value to a cell.
    CurrentMonth = NewMonth ' be sure and assign the CurrentMonth to the current month.
    End If
    End Sub
  • Dec 11, 2009, 09:23 AM
    rsdjimbie

    Thank you,
    Did get around the problem using a hiden (Month) formula.
    ie: NOW() - (Month)<>0 = set value to 0.

  • All times are GMT -7. The time now is 02:17 AM.