424需要错误对象

时间:2016-02-23 17:59:11

标签: excel vba excel-vba

此功能仅用于运行一大堆工作表并清除旧数据并将其复制到历史范围,以便为实际数据分析和传输做准备。当我尝试重置范围对象以继续执行工作表中单元格的清除时,我得到“运行时错误'424':需要对象。

我特别为这个错误感到困惑,因为它适用于它之前的两个对象没有问题。拼写似乎是正确的。有任何想法吗?错误发生在行Set historStartRangePuts = historStartRangePuts.Offset(0,10)

上函数底部的Else语句中
    Sub ClearTransferHistoricalOI(oiSheet As Worksheet)

    Dim continue As Boolean
    Dim startRangePuts As Range, startRangeCalls As Range, historStartRangePuts As Range, historStartRangeCalls As Range
    Dim newStartRangePuts As Range, newstartRangeCalls As Range, newHistorStartRangePuts As Range, newHistorStartRangeCalls As Range
    Dim columnCounter As Integer

    'Sets start range
    Set startRangePuts = oiSheet.Cells(5, 1)
    Set startRangeCalls = oiSheet.Cells(5, 3)
    Set historStartRangePuts = oiSheet.Cells(5, 6)
    Set historStartRangeCalls = oiSheet.Cells(5, 8)

    continue = True

    oiSheet.Activate

    'Attempts to catch already cleared sheets
    If IsEmpty(startRangePuts) = True Then
        continue = False

    End If

    Do While continue = True

        'Clears puts and calls historical data
        oiSheet.Range(historStartRangePuts, historStartRangePuts.Offset(0, 1)).Select
        oiSheet.Range(Selection, Selection.End(xlDown)).ClearContents

        oiSheet.Range(historStartRangeCalls, historStartRangeCalls.Offset(0, 1)).Select
        oiSheet.Range(Selection, Selection.End(xlDown)).ClearContents

        'Transfers puts and calls current data to historical data cols
        'Puts
        oiSheet.Range(startRangePuts, startRangePuts.Offset(0, 1)).Select
        oiSheet.Range(Selection, Selection.End(xlDown)).Cut historStartRangePuts
        'Calls
        oiSheet.Range(startRangeCalls, startRangeCalls.Offset(0, 1)).Select
        oiSheet.Range(Selection, Selection.End(xlDown)).Cut historStartRangeCalls

        If IsEmpty(startRangePuts.Offset(0, 10)) = True Then
            continue = False

        Else

            Set startRangeCalls = startRangeCalls.Offset(0, 10)
            Set startRangePuts = startRangePuts.Offset(0, 10)
            Set historStartRangePuts = startRangePuts.Offset(0, 5)
            Set historStartRangeCalls = startRangeCalls.Offset(0, 5)


        End If


    Loop


End Sub

1 个答案:

答案 0 :(得分:2)

您收到错误,因为没有为变量分配任何内容。这里 -     '清除放置并调用历史数据             oiSheet.Range(historStartRangePuts,historStartRangePuts.Offset(0,1))。选择             oiSheet.Range(Selection,Selection.End(xlDown))。选择             Selection.ClearContents

        oiSheet.Range(historStartRangeCalls, historStartRangeCalls.Offset(0, 1)).Select
        oiSheet.Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents

你失去了histor范围的定义。所以当你尝试

Set historStartRangePuts = historStartRangePuts.Offset(0, 10)
Set historStartRangeCalls = historStartRangeCalls.Offset(0, 10)

没有什么可以参考的。 认为是因为clearcontents返回variant - 这是一个对象。

这是发生在这里 -

    historStartRangePuts.Select
    ActiveSheet.Paste

删除该程序,您会看到Puts有效,但Calls仍然失败。