[英]Tallying a value into a variable conditionally based on cell inputs
I am working on a task where I am running tests at different temperatures but the test isn't always run in order so I've been tracking the temperature and the time overall time in the test.我正在执行一项任务,我在不同的温度下运行测试,但测试并不总是按顺序运行,所以我一直在跟踪温度和测试中的总时间。 Afterwards I go back and manually check how much time was spent at each temperature.
之后我 go 返回并手动检查在每个温度下花费了多少时间。 The code below shows how far I've gotten thus far but I'm not sure how to handle the empty cells that occur during the weekends when I am not able to tell record the time.
下面的代码显示了到目前为止我已经走了多远,但我不确定如何处理周末发生的空单元格,因为我无法告诉记录时间。
In my example attached, for 'Test 1' I expect to get total of 123 for 'Ambient' from (13-0)+(61-13)+(221-203)+(265-221), however it's outputting 358. Looking for some input on where I'm going wrong here.在我附加的示例中,对于“测试 1”,我希望从 (13-0)+(61-13)+(221-203)+(265-221) 中获得“环境”的总数 123,但是它输出 358 . 寻找一些关于我在这里出错的地方。 Thanks in advance!
提前致谢!
Here's the code I've written so far:这是我到目前为止编写的代码:
Sub Calculate_Time_at_Temp()
Dim ambient As Double, cold_neg_20 As Double, humidity As Double, hot_60 As Double, cold_neg_30 As Double, hot_80 As Double, rng As Range, cell As Range
Sheets("Sheet1").Activate
Set rng = Range("X5")
For Each cell In Sheets("Sheet1").Range("C8:W8")
'If IsNumeric(cell.Offset(-3, 0).Text) Then
If cell.Value = "Ambient" Then
ambient = ambient + (cell.Offset(-3, 0).Value - cell.Offset(-3, -1).Value)
rng.Value = ambient
End If
'End If
Next
End Sub
I just added a shift value to get the previous column: -3 if Saturday / Sunday, -1 else.我只是添加了一个移位值来获取上一列:如果是星期六/星期日,则为 -3,否则为 -1。 Assuming your dates are located in 1rst row (Cells(1,x)).
假设您的日期位于第一行(单元格(1,x))。 I'm unable to test it without your file.
没有您的文件,我无法对其进行测试。
Sub Calculate_Time_at_Temp()
Dim ambient As Double, cold_neg_20 As Double, humidity As Double, hot_60 As Double, cold_neg_30 As Double, hot_80 As Double, rng As Range, cell As Range
Dim iShift As Integer
Dim rRangeDate As Range
Sheets("Sheet1").Activate
Set rng = Range("X5")
For Each cell In Sheets("Sheet1").Range("C8:W8")
Set rRangeDate = Sheets("Sheet1").Cells(1, cell.Column)
iShift = IIf(Weekday(CDate(rRangeDate.Value), vbMonday) > 5, 3, 1)
If cell.Value = "Ambient" Then
ambient = ambient + (cell.Offset(-3, 0).Value - cell.Offset(-3, -iShift).Value)
rng.Value = ambient
End If
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.