我必须对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
贯穿第一张纸的所有单元格。对于第一张纸中的每个单元格都会遍历第二张纸上的所有单元格。使用datediff检查匹配的日期。这不是太棘手。我不知道stackoverflow.com是否适合您的“问题”。此外,我无法打开您的示例文件。它被阻止或者其他;-) –