VBA从循环中的变量检索单元格值

VBA从循环中的变量检索单元格值,vba,excel,Vba,Excel,从VBA中的宏获取所需的数据时,我遇到了一些问题。我对VBA很陌生,所以我可能错过了一些更容易的选择,但就我的一生而言,我找不到它 因此,我需要能够做到以下几点: 如果datediff小于60,则提取单元格的值。我使用今天的日期-一年,并将其设置为变量LDate。将LDate与单元格值进行比较(这些单元格将始终是日期,因此我认为不需要检查它们是否是日期)。如果单元格值为将所有“ActiveCell”引用更改为“cell”: PriExInf = Range("A" & Cell.Row)

从VBA中的宏获取所需的数据时,我遇到了一些问题。我对VBA很陌生,所以我可能错过了一些更容易的选择,但就我的一生而言,我找不到它

因此,我需要能够做到以下几点:


如果datediff小于60,则提取单元格的值。我使用今天的日期-一年,并将其设置为变量LDate。将LDate与单元格值进行比较(这些单元格将始终是日期,因此我认为不需要检查它们是否是日期)。如果单元格值为将所有“ActiveCell”引用更改为“cell”:

PriExInf = Range("A" & Cell.Row)
AltExInf = Range("B" & Cell.Row)
ExNum = Range("C" & Cell.Row)
这是因为“ActiveCell”引用了工作表中当前的“active”单元格,并且在任何“Select”语句之后都会发生变化


当您使用“Cell”作为循环变量在一个范围内循环时,这就是您需要使用的参考

非常感谢,这确实帮助我从循环中获得了更多信息。尽管我在获取这些数据的单元格值时仍然有问题
For Each Cell in Example_rng 'This same loop is used 3 other times with the only difference being the variables

    ExampleStart = Cell.Value
    If DateDiff("d", LDate, ExampleStart) <= 60 Then
        ExampleHold = Cell.Value 'This is the most important value that I need to retrieve. It determines the content of my email.
        PriExInf = Range("A" & ActiveCell.Row)
        AltExInf = Range("B" & ActiveCell.Row)
        ExNum = Range("C" & ActiveCell.Row)
        Cell.Interior.Color = RGB(255, 200, 205)
    ElseIf DateDiff("d", LDate, ExampleStart) > 60 And DateDiff("d", LDate, ExampleStart) < 180 Then
        Cell.Interior.Color = RGB(253, 251, 194)
    ElseIf DateDiff("d", LDate, ExampleStart) >= 180 Then
        Cell.Interior.Color = RGB(198, 250, 194)
    End If

Next Cell
Dim Example_rng As Range
Set Example_rng = Worksheets("sheet1").Range("D2:D87")

Dim Example1_rng As Range
Set Example1_rng = Worksheets("sheet1").Range("E2:E87")

Dim Example2_rng As Range
Set Example2_rng = Worksheets("sheet1").Range("F2:F87")

Dim Example3_rng As Range
Set Example3_rng = Worksheets("sheet1").Range("G2:G87")
PriExInf = Range("A" & Cell.Row)
AltExInf = Range("B" & Cell.Row)
ExNum = Range("C" & Cell.Row)