嵌套的“For Each”不适用于VBA

时间:2014-07-06 23:02:30

标签: excel vba excel-vba

我试图为每个'运行一个非常简单的嵌套'在VBA;但是语法错误。当我在第二个For-Each(PropFromOutput = O.Value)之后删除该行时,它可以正常工作。我哪里做错了? 谢谢。

Sub CompareAndMark()
Dim Linko As Worksheet
Dim Output As Worksheet
Dim PropFromLinko As String
Dim PropFromOutput As String

For Each L In Linko.Range("B2:B69").Cells
   PropFromLinko = L.Value

   For Each O In Output.Range("A2:A69").Cells
   PropFromOutput = O.Value

   Next O
Next L
End Sub

1 个答案:

答案 0 :(得分:2)

Sub CompareAndMark()
Dim Linko As Worksheet
Dim Output As Worksheet
Dim PropFromLinko As String
Dim PropFromOutput As String

Set Linko = ActiveSheet
Set Output = ActiveSheet

For Each L In Linko.Range("B2:B69").Cells
   PropFromLinko = L.Value

   For Each O In Output.Range("A2:A69").Cells
   PropFromOutput = O.Value

   Next O
Next L
End Sub

这将编译并运行OK(尽管LO值为空,但我不知道您的完整代码)。您失败的原因是因为您从未设置Worksheet Linko& Output设置为。请根据需要编辑分配给他们的工作表,以使其适合您