根据相关值将单元格设置为等于其他单元格

时间:2015-12-22 11:32:25

标签: excel vba loops

我试图根据他们在不同时间接近的读数来联系。在两个不同的工作表(“质量平衡”和“平均值”)中有一个时间列,我想根据这些数字的接近时间来复制“平均值”中的数字以粘贴“质量平衡”(参见图片)。

我遇到问题的部分是最终的If语句。出于某种原因,它将“平均值”中的最终值粘贴到“质量平衡”中的所有行中,而不是粘贴具有最接近时间的值。

变量描述:

mbv1& 2是“质量平衡”中的初始和最后时间

mbd1& 2是mbv1&的初始和最后一行。 2“质量平衡”

avgd1& 2是“平均值”中的初始和最后时间

Explanation Image

Dim c1 As Double
For o = mbv1 To mbv2
    For n = mbd1 To mbd2
        For m = avgd1 To avgd2
            For i = 0 To 40
                If Abs(Cells(m + i, 1) - o) < Abs(Cells(m + i + 1, 1) - o) Then
                    c1 = i + m
                    Sheets("Mass Balance").Cells(n, 3) = Sheets("Averages").Cells(c1, 4)
                    Sheets("Mass Balance").Cells(n, 10) = Sheets("Averages").Cells(c1, 6)
                Else
                End If
            Next
        Next
    Next
Next

1 个答案:

答案 0 :(得分:0)

我创建了一个如下所示的示例工作表:

 |         A         |         B          | C |       D      |       E       |
-------------------------------------------------------------------------------------------
1| Mass Balance Data | Mass Balance Times |   | Average Data | Average Times |
-------------------------------------------------------------------------------------------
2|                   |         13         |   |       1      |       10      |
3|                   |         22         |   |       2      |       20      |
4|                   |         31         |   |       3      |       30      |
5|                   |        ...         |   |     ...      |      ...      |

由于以下代码是使用简单数据完成的,因此您需要为自己的代码更改一些内容,但它对我的上述数据有用。 这个想法是总共有2个循环。你循环每个质量平衡时间。对于每个质量平衡时间循环,通过整个平均时间列。找到与质量平衡时间匹配的最接近时间后,复制数据值并将其粘贴到该特定时间的质量平衡数据中。

Option Explicit

Sub dataCollect()

'MBr stands for Mass Balance Row
'Each of these represent the row and col of a cell with time values
Dim MBr, MBc As Integer
Dim AVGr, AVGc As Integer
Dim minRow, minCol As Integer

Dim minDiff As Integer
Dim mbTime As Integer
Dim avgTime As Integer

Dim currDiff As Integer

'start off with beginning row and column of mass balance times and average times
MBc = 2
AVGc = 5
For MBr = 2 To 10

    'set minDiff to be a really high number
    'so that the first difference value between average and mass balance times
    'will be saved as the minimum difference value (used for if statement later)
    minDiff = 10000

    'loop through each average time
    'find and save the row and column of the cell with the closest time to mass balance time
    For AVGr = 2 To 10

        'mass balance time value
        mbTime = Cells(MBr, MBc).Value

        'average time value
        avgTime = Cells(AVGr, AVGc).Value

        'set current difference value
        currDiff = Abs(avgTime - mbTime)

        'if the difference in times is smaller than the last recorded difference
        'then save the row and column of the current average time cell
        'and set the new minimum (minDiff) value to be the current diff (currDiff) value
        If (currDiff < minDiff) Then
            minRow = AVGr
            minCol = AVGc
            minDiff = currDiff
        End If
    Next

    'now set the mass balance value to be the average value with the closest time
    'minCol - 1 and MBc -1     grabs the data value assosiated with that time
    Cells(MBr, MBc - 1) = Cells(minRow, minCol - 1).Value
Next
End Sub