在VBA中发生错误

错误:对象variables或未设置块variables

CODE:

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

如果Find操作没有find任何东西,那么pos将是Nothing

你需要一个守卫条款:

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