Excel - 仅使用整数来获取范围

时间:2017-03-22 12:35:02

标签: excel vba excel-vba excel-2013

    Dim cell As Range
    Dim agentRange As Range

    Set agentRange = ThisWorkbook.Worksheets("OtherSheet").Range(Range("S9").value, Range("T9").value)

    For Each cell In agentRange

        Dim col As Long
        col = cell.column

        Dim cellStart As Range
        ***cellStart = ThisWorkbook.Worksheets("OtherSheet").Cells(3, col))***

       'Do Stuff

    Next cell

所以我的cellStart分配有问题。当我尝试使用编写的代码时,我收到一个错误。

Run-time error '91'

Object Variable or With block variable not set

1 个答案:

答案 0 :(得分:2)

错误的原因是cellStartRange

因此,您需要Set使用:

Set cellStart = ThisWorkbook.Worksheets("OtherSheet").Cells(3, col)

注意:您最后有一个“额外”结束括号,您有2 ))而非)