Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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)</p> <p>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:</p> <pre><code>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 &gt; 24 Then intHours = intHours - 24 dtDate = dtDate + 1 End If Next End Sub </code></pre> <p>As you mention you start on a date, therefore I start with a date (no time) in the InputBox.</p> <p>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. </p> <p>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:</p> <pre><code>b.Offset(0, 1).Value2 = dtDate + TimeSerial(intHours +1, 0, 0) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload