[英]VBA Copying columns to matching date in excel calender
I am attempting to learn some vba programming for excel, Long story short 我正在尝试为Excel学习一些vba编程,长话短说
I have a machine using an allen bradley plc, I have created a program in the plc to record hourly run statitistics, I have managed to get these to update live into an excel sheet, it uploads each hour for 24 hours. 我有一台使用Allen Bradley plc的机器,我在plc中创建了一个程序来记录每小时运行的统计信息,我设法将这些信息实时更新到excel工作表中,每小时上载24小时。 The machine runs on 3 shifts 6am to 2pm, 2pm to 10 pm, 10 pm to 6 am.
机器按3个班次运行,从早上6点到下午2点,从下午2点到10点,从晚上10点到6点。 In the factory we class each day as 6am to 6am.
在工厂,我们每天从早上6点到早上6点上课。
I have written the following code, which copies the values from the plc and pastes them to the matching date, cell "c10" contains =today() then on sheet 2 it will paste the values to a calender under the matching date. 我编写了以下代码,该代码从plc复制值并将其粘贴到匹配日期,单元格“ c10”包含= today(),然后在工作表2上,它将值粘贴到匹配日期下的压延机中。
this is now working fine however i would like to change it so that under each date it contains 6am to 6 am values rather than 24 hours worth. 现在,它工作正常,但是我想更改它,以便在每个日期下它包含上午6点至上午6点的值,而不是24小时的值。
the issue i have is that cell c10 (todays date) will update after 12am and therefore the paste destination will change. 我的问题是单元格c10(今天的日期)将在凌晨12点之后更新,因此粘贴目标将更改。
heres my code 这是我的代码
Private Sub work_test()
'set variables
Dim Day As Date
Dim rfound As Range
Dim frow, fcol As Integer
Dim sh1, sh2 As Worksheet
'set sheets
Set sh1 = Sheets("sheet1")
Set sh2 = Sheets("sheet2")
'sets day as current day, finds matching day in sheet2
Day = sh1.Range("c10")
Set rfound = sh2.Range("7:11").Find(Day, LookIn:=xlValues)
If Not rfound Is Nothing Then
frow = rfound.Row
fcol = rfound.Column
sh1.Range("c11:c34").Copy sh2.Cells(9, fcol)
Else
MsgBox "No match found"
End If
'runs timer
Call timer
End Sub
Sub timer()
'repeats cell update timer
Application.OnTime Now + TimeValue("00:01:00"), "work_test"
End Sub
Hope someone can help, not looking for a complete solution, just a bit of help in the correct direction 希望有人能提供帮助,而不是寻找完整的解决方案,只是向正确的方向提供一点帮助
Thanks 谢谢
These are two of many ways achieving what yo want: 以下是实现您想要的多种方式中的两种:
1.- Replace formula in C10
with this one: 1.-用以下公式替换
C10
公式:
= TODAY() + IF( NOW() - TODAY() < TIME(6,0,0) , -1 , 0 )
Formula above validates the time and it less that 06:00:00 then rest one to the date. 上面的公式验证时间,并且时间少于06:00:00,然后再休息一个到日期。 Thus anything between midnight and 06 AM will be taken as the day before
因此,午夜至凌晨06点之间的任何时间都将被视为前一天
2.- In your code replace this line: 2.-在您的代码中替换以下行:
Day = sh1.Range("c10")
with this: 有了这个:
Day = Date + IIf(Time < TimeSerial(6, 0, 0), -1, 0)
Same as in point 1 above, only that since you are using VBA there is no need to have the date as a formula in the worksheet, the date of the machine can be obtain that directly in the VBA and proceed from there. 与上面的第1点相同,只是因为您使用的是VBA,所以不需要在工作表中使用日期作为公式,因此可以直接在VBA中获取机器的日期并从那里开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.