Excel 基于相关值将单元格设置为与其他单元格相等

Excel 基于相关值将单元格设置为与其他单元格相等,excel,vba,loops,Excel,Vba,Loops,我试图根据它们在时间上的接近程度,将不同时间的读数联系起来。在两个不同的表格(“质量平衡”和“平均值”)中有一个时间列,我想根据这些数字的时间接近程度,将“平均值”中的数字复制到“质量平衡”中(参见图) 我遇到的问题是最终的If语句。出于某种原因,它将“平均值”中的最终值粘贴到“质量平衡”中的所有行中,而不是粘贴时间最接近的值 变量描述: mbv1和2是“质量平衡”中的初始和最终时间 mbd1和2是“质量平衡”中mbv1和2的初始行和最终行 avgd1和AVGD2是“平均值”中的初始和最终时间

我试图根据它们在时间上的接近程度,将不同时间的读数联系起来。在两个不同的表格(“质量平衡”和“平均值”)中有一个时间列,我想根据这些数字的时间接近程度,将“平均值”中的数字复制到“质量平衡”中(参见图)

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

变量描述:

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

mbd1和2是“质量平衡”中mbv1和2的初始行和最终行

avgd1和AVGD2是“平均值”中的初始和最终时间

尺寸c1为双精度
对于o=mbv1至mbv2
对于n=mbd1至mbd2
对于m=avgd1至avgd2
对于i=0到40
如果Abs(单元(m+i,1)-o)
我创建了一个示例工作表,如下所示:

 |         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
选项显式
子数据收集()
'MBr代表质量平衡行
'每一个都表示具有时间值的单元格的行和列
Dim MBr,MBc为整数
Dim AVGr,AVGc为整数
Dim minRow,minCol为整数
作为整数的Dim-minDiff
将mbTime设置为整数
Dim avgTime为整数
Dim currDiff作为整数
'从质量平衡时间和平均时间的开始行和开始列开始
MBc=2
AVGc=5
对于MBr=2到10
“将minDiff设置为一个非常高的数字
'因此,平均值和质量平衡时间之间的第一个差值
'将另存为最小差值(稍后用于if语句)
minDiff=10000
'循环通过每个平均时间
'查找并保存与物料平衡时间最接近的单元格的行和列
对于AVGr=2到10
“质量平衡时间值”
mbTime=单元(MBr,MBc).值
'平均时间值
avgTime=单元格(AVGr,AVGc).Value
'设置当前差值
currDiff=Abs(avgTime-mbTime)
'如果时间差小于上次记录的时间差
'然后保存当前平均时间单元格的行和列
'并将新的最小(minDiff)值设置为当前的差异(currDiff)值
如果(currDiff
因此,在“平均值”表中,一列中有数据点,每个数据点都有一个与其相关的时间(在另一列中)。在“质量平衡”表中也有一列表示时间,但时间点与“平均值”表中的时间点都不同。假设“质量平衡”表中的时间为12:00:00,而“平均值”表中的时间为11:20:00和13:40:00。由于11:20:00接近12:00:00,我希望表格“平均值”中与11:20:00点位于同一行的数据点复制到表格“质量平衡”中与12:00:00相同的行中,因为“平均值”和“质量平衡”是两个独立的表格,我正在努力找出如何使用您的方法。这种方法仍然适用于正确的情况?上面有:表(“质量平衡”)。单元格(n,3)你应该能够使用与我拉单元格(MBr,MBc-1)=相同的符号。。。将变成薄片(“质量平衡”)。电池(MBr,MBc-1)=。。。MBr和MBc只是对质量平衡行和列的引用(特别是时间列,然后MBc-1引用数据列,因此只需根据需要进行替换。由于我的数据表中质量平衡时间的列为2,我在上面设置了MBc=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