简体   繁体   English

根据单元格输入有条件地将值计入变量

[英]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.

相关问题 动态范围要根据单元格值进行条件格式化 - Dynamic range to be conditionally formatted based of cell value 是否可以基于另一个单元格中的值有条件地使用数据条格式化一个单元格? - Is it possible to conditionally format one cell with a databar based on a value in another cell? 基于单元格值的可变数组 - Variable Array based on cell value 根据单元格值有条件清除一行中单元格范围的内容 - Conditionally clear contents of range of cells in a row based on cell value Excel:根据自己的值有条件地格式化每个单元格 - Excel: Conditionally formatting each cell based on its own value Excel VBA用于根据单元格值有条件地隐藏或隐藏工作表 - Excel VBA for Unhiding or Hiding a Worksheet Conditionally Based on Cell Value 根据VBA中其他单元格的值,有条件地格式化单元格的循环范围 - Conditionally formatting a looped range of cells based on value in other cell in VBA EXCEL-宏可根据单元格值有条件地合并行 - EXCEL - Macro to conditionally merge rows based on cell value 如何根据最底端的重复值为单元格着色? - How conditionally to colour a cell based on the most bottom repeated value? 如何根据每个单元格的值和固定单元格的值有条件地格式化VBA中的列? - How do I conditionally format a column in VBA based on each cell's value and a fixed cell's value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM