2015-12-22 33 views
0

我想涉及設置細胞的基礎上,如何在時間上接近他們,讀數分別在不同時間拍攝。在兩個不同的表(「質量平衡」和「平均值」)有一個時間列,我想從「平均數」拷貝數基於如何接近這些數字的時間(見PIC)的「質量平衡」進行粘貼。等於基於相關值的其他細胞

我遇到的麻煩的部分是最後的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 

回答

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 
+0

所以在Sheet「Averages」中,列中有數據點,每個數據點都有一個與其關聯的時間(在另一列中)。在「質量平衡」表中也有一個時間列,但時間點與「平均值」中的時間點不同。 假設在12:00:00工作表中存在「質量平衡」時間,而在「平均」工作表中,時間爲11:20:00和13:40:00。自11時20分00秒接近12:00:00,我想在表的「平均數」,也就是在同一行中爲11時20分00秒點複製到工作表「質量平衡」的數據點到相同行作爲12:00:00 – lorims

+0

我竭力要弄清楚如何使用你的方法,因爲「平均值」和「質量平衡」是兩個不同的表。這種方法仍然可以正確工作? – lorims

+0

上面有:表格(「質量平衡」)單元格(n,3)您應該能夠使用相同的符號,我將單元格(MBr,MBc - 1)= ...變爲表格(「質量平衡「).Cells(MBR,MBC -1)= ... MBR和MBC是質量平衡的行和列(特別是時間列,然後MBC -1指的是數據列只是引用,以便只需更換該需要。由於我的數據表中列的質量平衡時間爲2,因此我在上面設置了MBc = 2。) – Ag71191