2014-06-17 46 views
0

我需要VBA幫助根據多個條件替換某一列的內容。VBA根據多個條件(數量和日期)更改單元格值

如果Sheet1中的擴展列中的值位於Sheet2 中的擴展名列中,並且Sheet1中的日期等於或落在Sheet2中的Date From和Date To之間,則將Sheet1中的Number替換爲Sheet2中對應的Number,否則,什麼也不做。

下面是詳細的截圖。 http://prntscr.com/3tnf6t

並鏈接到帶有示例條目的文件。 http://1drv.ms/1skLYJR

我試圖錄制一個宏來做自動過濾器,但是不知道如何輸入符合所有條件的記錄的相應編號。

任何幫助將不勝感激。

謝謝!

+1

貫穿第一張紙的所有單元格。對於第一張紙中的每個單元格都會遍歷第二張紙上的所有單元格。使用datediff檢查匹配的日期。這不是太棘手。我不知道stackoverflow.com是否適合您的「問題」。此外,我無法打開您的示例文件。它被阻止或者其他;-) –

回答

0

我必須對Excel進行一些小的修改才能使其正常工作。我使用Excel函數TODAY()替換了Sheet2中的Today,它只返回當前日期。之後這個快速和骯髒的解決方案應該解決您的問題。

Sub checkAndReplace() 

Dim currentRowS1, currentRowS2 As Integer 

Range("B1:A" + CStr(ThisWorkbook.Worksheets("Sheet1").UsedRange.Count) + ",A1:A" + CStr(ThisWorkbook.Worksheets("Sheet2").UsedRange.Count)).Select 

For currentRowS1 = 2 To ThisWorkbook.Worksheets("Sheet1").UsedRange.Count 
    For currentRowS2 = 2 To ThisWorkbook.Worksheets("Sheet2").UsedRange.Count 
    If ThisWorkbook.Worksheets("Sheet1").Range("B" & currentRowS1).Text = ThisWorkbook.Worksheets("Sheet2").Range("A" & currentRowS2).Text Then 
     If DateDiff("d", ThisWorkbook.Worksheets("Sheet1").Range("A" & currentRowS1), ThisWorkbook.Worksheets("Sheet2").Range("B" & currentRowS2)) <= 0 And DateDiff("d",  ThisWorkbook.Worksheets("Sheet1").Range("A" & currentRowS1), ThisWorkbook.Worksheets("Sheet2").Range("C" & currentRowS2)) >= 0 Then 
     ThisWorkbook.Worksheets("Sheet1").Range("C" & currentRowS1).Value = ThisWorkbook.Worksheets("Sheet2").Range("D" & currentRowS2).Value 
     End If 
    End If 

    Next 
Next 

End Sub 
+0

謝謝哈里斯! – user3468115

相關問題