2015-04-24 36 views

回答

0

VBA入門可能有點令人生畏。特別是當你試圖解決一些感覺應該很簡單的事情時。通常情況下,解釋簡單的事情會導致代碼中最大的麻煩。

這個並不難,但是如果你不熟悉For循環以及VBA如何引用單元格和範圍,以及各種對象和變量類型......它可能會非常棘手。下面是一些代碼,可以幫助你找到你想要的東西,或者至少讓你接近你。

Sub findValueForMaxDate() 
    Dim sheetARow as Range 
    Dim sheetBRow as Range 
    Dim maxDate As Date 
    Dim searchValue as string 

    'Loop through all of the rows in SheetA 
    For each sheetARow in Sheets("SheetA").Range("C5:C1000").Rows 

     'Set maxdate to something really low 
     maxDate = '01/01/1900' 

     'While we are on each row in SheetA 
     ' loop through ALL the rows in SheetB 
     'Each sheetA row will cause us to loop 
     ' all of the SheetB Rows 
     For Each SheetBRow in Sheets("SheetB").Range("A1:A10000").Rows 

      'Compare indexes and dates 
      If SheetARow.Cells(1,3).Value = SheetBRow.Cells(1,1).value _ 
       AND SheetBRow.Cells(1,2).value > maxDate Then 

       'Store this maxDate so we can compare again 
       ' if we find another index match 
       maxDate = SheetBRow.Cells(1,2).value 

       'Store the value from sheetBRow 
       'This will be overwritten if we happen to find a 
       ' higher date. 
       searchValue = SheetBRow.Cells(1,3).value 
      End if 
     Next SheetBRow 

     'At this point the value in searchValue should be the one that corresponds 
     ' to the highest date. So write it out in Column D of Sheet1 for this rows 
     ' for which we are searching 

     SheetARow.Cells(1,4).value = searchValue 

    Next sheetARow 

End Sub 

這不是地球表面上最高效的東西,因爲它必須遍歷每個SheetA行的每個sheetB行。這基本上是995 * 10000循環。

更快的方法是首先按索引對SheetB進行排序,日期遞減。然後SheetB for循環只會獲取找到的第一個索引的值並退出for循環。還有其他的方法可能會更快,但這是一個很好的起點。

未來,當在StackOverflow上提出問題時,請顯示您迄今爲止編寫的代碼,並讓人們確切知道您卡在哪裏。一般來說,像「告訴我如何編寫此代碼」這樣的問題並沒有得到很好的迴應,並且會收到大量的反對票。更多的故障排除在這裏,少拿手。

+0

非常感謝。明天我會試一試。 '讓你知道 – vince

0

我想我會提供一個沒有嵌套循環的替代方案,因爲這是在您的窗口標題中快速路徑(Not responding)

如果您認識到您基本上想要執行查找,但需要過濾查找表的值,則此問題要容易得多。 Microsoft Scripting Runtime具有可用於構建查找表的Dictionary對象。

進行兩次傳遞 - 首先在SheetB上構建一個包含具有最高日期的每個索引的行的字典,第二次在SheetA上基於索引填充字典中的值。

添加引用(在VBE工具 - >引用...,然後在列表中找到一個檢查),並嘗試這樣的事:

Sub MaxValueLookup() 
    Dim sheetA As Worksheet 
    Dim sheetB As Worksheet 
    Dim maxes As Dictionary 
    Set sheetA = Sheets("SheetA") 
    Set sheetB = Sheets("SheetB") 
    Set maxes = New Dictionary 

    'Get your lookup table: 
    Dim index As Long 
    Dim key As Variant 
    For index = 1 To sheetB.UsedRange.Rows.Count 
     key = sheetB.Cells(index, 1) 
     'Check to see if you have the key already and it isn't empty. 
     If key <> vbNullString Then 
      If Not maxes.Exists(key) Then 
       'Store the row. 
       maxes.Add key, index 
      Else 
       'Already have it, check to see if the date is higher. 
       If sheetB.Cells(index, 2) > sheetB.Cells(maxes(key), 2) Then 
        maxes(key) = index 
       End If 
      End If 
     End If 
    Next index 
    'Now sheetA is simple... 
    For index = 5 To sheetA.UsedRange.Rows.Count 
     key = sheetA.Cells(index, 1) 
     If maxes.Exists(key) Then 
      'Put the latest value in column B 
      sheetA.Cells(index, 2) = sheetB.Cells(maxes(key), 3) 
     End If 
    Next index 
End Sub 
+0

非常感謝。明天我會試一試。 '讓你意識到 – vince

相關問題