Results 1 to 3 of 3

Adding or subtracting minutes from Now in VBA Code

This is a discussion on Adding or subtracting minutes from Now in VBA Code within the Excel Questions forums, part of the Question Forums category; I have the following code and it works great it was given to me by some of the board members. ...

  1. #1
    Board Regular
    Join Date
    Sep 2004
    Posts
    132

    Default Adding or subtracting minutes from Now in VBA Code

    I have the following code and it works great it was given to me by some of the board members. Is ther a way to insert additional language in it that will add minutes to the Now time. Also the same in reverse is there language that will Subtract minutes from the Now? I know nothing about code but I am trying to learn. The following is the code:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Intersect(Target, Union(Range("B9:B44"), Range("D9:D44"), Range("F9:F44"), Range("H9:H44"), Range("J9:J44"), Range("K9:K44"))) Is Nothing Then Exit Sub
    If Selection.Rows.Count > 1 Then Exit Sub
    If Selection.Value <> "" Then Exit Sub
    With Selection
    .Value = Now
    .NumberFormat = "hh:mm"
    End With
    End Sub

  2. #2
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,949

    Default

    Use the TimeSerial function, like

    .Value = Now + TimeSerial(0, 10, 0)

    to add 10 minutes. To substract, just change the plus with a minus.
    Regards,

    Juan Pablo González
    http://www.juanpg.com

  3. #3
    Board Regular
    Join Date
    Sep 2004
    Posts
    132

    Default

    Thanks that worked great. How would I seperate it out I want to subtract minutes from B9:B44 Only and the I want to add 1 minute to H9:H44. Is that difficult to do given the code I have?

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com