Time + 15 minutes & repeat

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm using the following code.

VBA Code:
Sub Time()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim sdt As Date
Dim rng As Range

Set ws1 = Worksheets(7)
Set rng = ws2.Range("A4")

sdt = ws1.Range("K1").Value

rng.Value = sdt

End Sub

It grabs a date with time from ws1 K1 and places it in ws2 A4. K1 is formatted like "06/01/2022 11:30".

How can I get the code to loop round and add 15 minutes for each cell in A? For example, A5 would show "06/01/2022 11:45", A6 would be "06/01/2022 12:00" and, importantly, A99 would be "07/01/2022 06:15" (23 hours an 45 minutes from the start date and time).

I should note, the times I have given are all just examples so using the NOW function won't be useful. The date and time in ws1 K1 are variable

Thanks!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi all,

I'm using the following code.

VBA Code:
Sub Time()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim sdt As Date
Dim rng As Range

Set ws1 = Worksheets(7)
Set rng = ws2.Range("A4")

sdt = ws1.Range("K1").Value

rng.Value = sdt

End Sub

It grabs a date with time from ws1 K1 and places it in ws2 A4. K1 is formatted like "06/01/2022 11:30".

How can I get the code to loop round and add 15 minutes for each cell in A? For example, A5 would show "06/01/2022 11:45", A6 would be "06/01/2022 12:00" and, importantly, A99 would be "07/01/2022 06:15" (23 hours an 45 minutes from the start date and time).

I should note, the times I have given are all just examples so using the NOW function won't be useful. The date and time in ws1 K1 are variable

Thanks!
I should probably have said, I'm aware that what I have written so far is a complicated way of doing what it currently does - but I'm expecting the solution to extend the rng variable to cover A4:A99 and then maybe use For Each? I'm a bit lost when it comes to creating code like that
 
Upvote 0
Hi Luke,

Check below code.

VBA Code:
Sub Time()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim sdt As Date
Dim rng As Range, rowno As Integer

Set ws1 = Worksheets(7)
'Set ws2 = Sheets("Sheet2")
Set rng = ws2.Range("A4")

sdt = ws1.Range("K1").Value

For rowno = 4 To 10
    rng.Value = sdt
    sdt = sdt + (15 / 60 / 24)
    Set rng = rng.Offset(1, 0)
Next

End Sub
 
Upvote 0
Solution
Hi Luke,

Check below code.

VBA Code:
Sub Time()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim sdt As Date
Dim rng As Range, rowno As Integer

Set ws1 = Worksheets(7)
'Set ws2 = Sheets("Sheet2")
Set rng = ws2.Range("A4")

sdt = ws1.Range("K1").Value

For rowno = 4 To 10
    rng.Value = sdt
    sdt = sdt + (15 / 60 / 24)
    Set rng = rng.Offset(1, 0)
Next

End Sub
Works nicely! Thank you very much
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,047
Members
449,064
Latest member
scottdog129

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top