Results 1 to 4 of 4

Thread: macro question

  1. #1

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    macro question

    I am trying to write a macro where it will write a value to a cell like A7 and then each new day I want it to add a number to the cell. So today it would write a number in A7 and then tomorrow go to A8 automatically. Is this possible?

    Thanks.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: macro question

    Not sure what the question is here.
    Are you trying to understand how to

    A/ Select the next cell down?
    or
    B/ Get the code to run everyday?

    For A/ you could use the following
    VB Code:
    1. Dim rngStartCell As Range
    2. Dim rngNextCell As Range
    3.  
    4.     'Set the starting position
    5.     Set rngStartCell = ThisWorkbook.Worksheets(1).Range("A7")
    6.    
    7.     With rngStartCell
    8.         If .Offset(1, 0).Value = "" Then
    9.             'Special Case handler for when there are
    10.             'no entries belwo the starting cell
    11.             Set rngNextCell = .Offset(1, 0)
    12.         Else
    13.             'All other cases - find the last used cell
    14.             'below the start cell and then select the
    15.             'next cell down
    16.             Set rngNextCell = .End(xlDown).Offset(1, 0)
    17.         End If
    18.     End With
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: macro question

    It is sort of B. Lets say today, May 26th it is run and it needs to put data into cell A7. When it is run the next day (May 30th) it will add one more to the previous cell that has data in it. Therefore, it would notice that the day has changed and check the cell A7 to see if it has data and if it does then A7 + 1 = A8 and put new data into that cell.

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: macro question

    OK, so we're part of the way there. Now we just need a mechanism to store the last date that the file was accessed. I would sugget using an Excel Named Rage for this purpose.

    Step 1/ Add a named range called LastUsedDate and makes its formula the following, you must inclued the equal sign and the quotes
    ="4/26/2006"

    Step 2/ Add the follwoing procedure to the workbook
    VB Code:
    1. Sub CheckLastUsed()
    2. Dim dLastDate As Date
    3. Dim lDaysDiffer
    4. Dim rngStartCell As Range
    5.    
    6.     'Find the last used date
    7.     dLastDate = Evaluate(ThisWorkbook.Names("LastUsedDate").Value)
    8.    
    9.     'Find the number of days since last use
    10.     lDaysDiffer = DateDiff("D", dLastDate, Now())
    11.    
    12.     'Only continue if it is more than one day
    13.     If lDaysDiffer > 1 Then
    14.    
    15.     'Set the starting position
    16.     Set rngStartCell = ThisWorkbook.Worksheets(1).Range("A7")
    17.    
    18.     With rngStartCell
    19.         If .Offset(1, 0).Value = "" Then
    20.             'Special Case handler for when there are
    21.             'no entries below the starting cell
    22.             .Offset(1, 0) = 1
    23.         Else
    24.             'All other cases - find the last used cell
    25.             'below the start cell and then update the
    26.             'next cell down with the next number
    27.             .End(xlDown).Offset(1, 0) = .End(xlDown).Value + 1
    28.         End If
    29.     End With
    30.    
    31.     'Update the named range with todays date
    32.     ThisWorkbook.Names("LastUsedDate").Value = "=""" & Format(Now(), "mm/dd/yyyy") & """"
    33.        
    34.     End If
    35. End Sub

    Step 3/ If you need this procedure to run automatically when the workbook opens then add a reference to the proc to the workbook_open event.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width