在VBA中获取错误

时间:2017-08-04 05:43:54

标签: excel vba excel-vba

错误:对象变量或未设置块变量

代码:

x.Worksheets.Add().Name = "ab"

x.Worksheets.Add().Name = "bc"

LastRow = x.Sheets("Summary").Columns("A").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row

Dim pos As Range

For j = 1 To LastRow

    Set pos = x.Sheets("Summary").Range("A1:X" & j).Find(What:="SAP_ALL", LookIn:=xlValues, LookAt:=xlWhole, _
                MatchCase:=False, SearchFormat:=False)

    a = pos.Column

Next j

错误在a = pos.Column

1 个答案:

答案 0 :(得分:1)

如果Find操作找不到任何内容,则pos将为Nothing

你需要一个保护条款:

If Not pos Is Nothing Then
    a = pos.Column
End If