虽然我已经在Excel VBA中设置了所有的variables,但是“Object required”错误

我已经设置了所有的variables,但我仍然不断收到错误“对象所需”在

For Each cll In OHLC 

这是我的代码:

 Sub hellohello() Dim i As Integer Dim OHLC, cll As Range Dim stoplossb, entrypriceb As Variant stoplossb = 1 entrypriceb = 1 i = 1 For i = 63 To 166 If Range("M" & i).Value = "buy" Then stoplossb = Range("X" & i) entrypriceb = Range("w" & i) Set OHLC = Range("B" & i & ":" & "E10000") ' Set twndlow = Range("K" & i & ":" & "K10000") End If For Each cll In OHLC If cll.Value < stoplossb Then Range("Y" & cll.Row) = cll.Value Exit For End If Next cll Next i End Sub 

我的错误是什么?

在结束之前,将OHLC设置为else语句中的空范围。

或者把它放在if块里面

 Sub hellohello() Dim i As Integer Dim OHLC, cll As Range Dim stoplossb, entrypriceb As Variant stoplossb = 1 entrypriceb = 1 i = 1 For i = 63 To 166 If Range("M" & i).Value = "buy" Then stoplossb = Range("X" & i) entrypriceb = Range("w" & i) Set OHLC = Range("B" & i & ":" & "E10000") ' Set twndlow = Range("K" & i & ":" & "K10000") For Each cll In OHLC If cll.Value < stoplossb Then Range("Y" & cll.Row) = cll.Value Exit For End If Next cll End If Next i End Sub 

你得到一个错误的原因是因为你的IF语句是不正确的,所以你永远不会把OHLC设置为一个范围。 由于范围从来没有设置范围等于没有。

卡洛斯说的是,你有一个从63到166的外循环,在循环中你有一个IF语句,你决定是否设置OHLC,但是你有一个内部循环,不pipe是否OHLC被设置在IF语句中。我认为你需要将End If移动到下一个Cll之后,你的内部FOR循环只会在设置了OHLC的时候运行

 Dim OHLC As Range, cll As Range