copy and paste based on a time in cell A1

BenGrobler

New Member
Joined
Apr 19, 2021
Messages
31
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi Guys

I have a running clock in an excel worksheet, here is the code:
Dim SchedRecalc As Date
Sub Recalc()
With Sheet8.Range("A1")
.Value = Format(Time, "hh:mm:ss AM/PM")
End With
Call SetTime
End Sub
Sub SetTime()
SchedRecalc = Now + TimeValue("00:00:01")
Application.OnTime SchedRecalc, "Recalc"
End Sub
Sub Disable()
On Error Resume Next
Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Recalc", Schedule:=False
End Sub
I need the value in cell E4 to be copied when the running clock reaches 09:00 am and paste it in D17
then when the clock reaches 10:00 am it must copy the value in E4 and paste it in D18...
i need to create a graph with this info..

Is there a vba code that i can insert it into the above to make it happen or a if statement to copy and paste??

Please help
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
try this:
VBA Code:
Sub Recalc()
With Sheet8.Range("A1")
.Value = Format(Time, "hh:mm:ss AM/PM")
End With
hr = Hour(Now)
If hr >= 9 Then
 Range("D17").Offset(hr - 9, 0) = Range("e4")
End If
Call SetTime
End Sub
 
Upvote 0
Thank you

How would the code look like for 10, 11, 12 etc until 16:00?
 
Upvote 0
I dont know if I am clear enough on this...
when it is 09:00 in the morning the value in Cell e4 is maybe 2580 then at 10:00 it is 4570
when the clock is getting to 09:00 cell e4 is copied and pasted in d17, at 10:00 e4 is copied again and pasted in d18 at 11:00 e4 is copied again and pasted in d19
this carries on until 16:00. the values in e4 will go up and down during the day and that is what i need to get done automatically onse the set time macro is running?

the code maybe is like this
if time in range a1 = 09:00 then range e4 .select
selection copy
range d17 . select
selection paste
end if
if time in range A1 = 10:00 then range e4 .select
selection copy
range d18. select
selection paste
 
Upvote 0
That is exactly what the code does, let me explain:
VBA Code:
hr = Hour(Now)   ' this line takes the hour from the current time
If hr >= 9 Then   ' if the hour is greater than 9 it does the next lines
 Range("D17").Offset(hr - 9, 0) = Range("e4")   ' this line uses Hr ( the hour) to calculate the offset down from D17, so when hr =9, the offset is zero so it writes into D17, when hr=10 the offset is(10-9) =1
                                                                          ' so it writes in D18, when hr= 11 offset = 2 so it wrties into D19
End If
Note the code copies that values into the location for entire 1 hour, so if you want the first value in the hour change code to
VBA Code:
hr = Hour(Now)
If hr >= 8 Then
 Range("D17").Offset(hr - 8, 0) = Range("e4")
End If
 
Upvote 0
Solution

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,091
Latest member
gaurav_7829

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