Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:48:07+00:00 2026-06-13T17:48:07+00:00

How do I add time in 24 hours to this macro I have I

  • 0

How do I add time in 24 hours to this macro I have I already created? Here is the Macro below. I need to add a time stamp to it and I am not sure of how.

Private Sub Workbook_Open()

Dim dtDate As Date

dtDate = InputBox("Date", , Date)

Set SelRange = Range("B:B")
For Each b In SelRange.Rows
b.Value = dtDate
b.Offset(0, 1).Value = dtDate + 1
Next
End Sub

I need the time to increment like for 24 hours base off the macro for this worksheet. I have an example of what I need to do. In this example the date and time is showing I need the time to increment in a 24 hour period.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:48:08+00:00Added an answer on June 13, 2026 at 5:48 pm

    First of all I recommend to always use Option Explicit. This can be enabled for future projects through your menu in the MS VB window: Tools>Options>Editor (tab)>Require Variable Declaration (tickbox)

    Now to business: If you want a range (B2:B25 and C2:C25 in this example) to be filled with date + time with 1 hour increments I recommend using something like the following:

    Option Explicit
    
    Private Sub Workbook_Open()
    
        Dim SelRange As Range
        Dim b As Range
        Dim dtDate As Date
        Dim intHours As Long
    
        'Enter the starting date and time
        dtDate = InputBox("Start date", , Date)
    
        'Initate our hours counter
        intHours = 0
    
        'Assign a worksheet range for our date-time range
        Set SelRange = Range("B2:B25")
    
    
        'Write the date-time range with 1 hour increments
        For Each b In SelRange
            b.Value2 = dtDate + TimeSerial(intHours, 0, 0)
            '24 hours later in the cell to the right of this
            b.Offset(0, 1).Value2 = dtDate + 1 + TimeSerial(intHours, 0, 0)
            intHours = intHours + 1
            'To avoid an overflow in the TimeSerial function the intHours are keps smaller than then Integer maximum
            If intHours > 24 Then
                intHours = intHours - 24
                dtDate = dtDate + 1
            End If
        Next
    
    End Sub
    

    As you mention you start on a date, therefore I start with a date (no time) in the InputBox.

    Now if you want this to be repeated only a limited times you can do this by limiting the range it applies to (B2:B25 in this case), the loop will stop when it reaches the bottom.

    It now automatically fills the C column with a date time 24 hours in the future, If you just want it to have the date in the future skip the Timeserial part in the line for the C column. If you want this to be 1 hour in the future then change the line to:

    b.Offset(0, 1).Value2 = dtDate + TimeSerial(intHours +1, 0, 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent hours trying to work this out, and spent a long time
I have a list of breakpoints which I want to add each time I
I need to set the color of the font every time I add text
I have a 24 hour time string (ie 16:30) and would like to add
I have records with a time value and need to be able to query
I have spent hours trying to figure this problem and i just can't, so
My application should have some changes after some time (in hours or days) and
I have been doing the booking form on this website here. http://offline.raileisure.com/ if you
I've been stuck with this for hours now. I think it's time for some
how to add 6 hours to this string? $parent = 2011-08-04 15:00:01; I think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.