"需要对象"虽然我已经在Excel VBA中设置了所有变量,但错误

时间:2016-10-11 01:23:57

标签: excel vba excel-vba

我设置了所有变量,但我仍然不断收到错误"需要对象"在

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

我的错误是什么?

4 个答案:

答案 0 :(得分:0)

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

或者将if放在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

答案 1 :(得分:0)

您收到错误的原因是因为您的IF语句不正确,因此您永远不会到达将OHLC设置为范围的行。由于范围从未设定,因此范围等于零。

答案 2 :(得分:0)

卡洛斯所说的是你有一个从63到166的外循环,在你的循环内你有一个IF语句,你决定是否设置OHLC然后你有一个内循环,无论是否运行或者不是OHLC被设置在IF语句中。我认为你需要将End If移动到下一个cll之后,那样你的内部FOR循环只会在OHLC设置时运行

答案 3 :(得分:0)

    hlineMinus = 0;
//for (var i = 0; i < 21; i++) {
    var doc = app.activeDocument;
imageWidth = activeDocument.width.as('px');
imageHeight = activeDocument.height.as('px');
imageWidth - 24;
imageHeight - 24;

hline = i+1 * 24;
hlineMinus = 0;
if(hline = 21*24)
hlineMinus += hline;
hline=hlineMinus;

    var bounds = [24*(i), 0, 24*(i+1), 24*(i+1)];
    doc.crop(bounds);

    //do the math to figure out how big you want it after resize
    //doc.resizeImage(newWidth, newHeight);

    //note this is saving over the original!!!!
       var opts, file;
        opts = new ExportOptionsSaveForWeb();
        opts.format = SaveDocumentType.PNG;
        opts.PNG8 = false;
        opts.quality = 100;
var d = i;
        pngFile = new File(doc.path + d + doc.name );
        app.activeDocument.exportDocument(pngFile, ExportType.SAVEFORWEB, opts);
//app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);  
//stepHistoryBack();

//}

function stepHistoryBack(){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID( "HstS" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Prvs" ));
    desc.putReference(charIDToTypeID( "null" ), ref);
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};