学习一些VBA。到目前为止,我已经构建这段代码应该让我(虽然它没有,还)做以下的事情:对象变量或变量未设置
- 获取在
"M" & i
细胞的数量(在第一次迭代是M5 )。 - 在A列中找到该号码。
- 一旦找到它,请将
PutHereIfFound
的值设置为与F6的值相同(因此为偏移量)。 - 如果找到一个数字,然后递增i,以便循环继续搜索M6,M7,...直到单元格M20。
它返回Run-Time Error 91
,其代表Object Variable or With Variable not set
。当我调试时,它指向Set PuthereIfFound
行。
这个错误的原因是什么?
Sub FindThis()
Dim FindThis As Range
Dim PutHereIfFound As Range
Dim i As Integer
Dim f As Integer
i = 5
f = 5
Do
Set FindThis = ActiveSheet.Range("M" & i)
Set PutHereIfFound = ActiveSheet.Range("N" & i)
With ActiveSheet.Range("A:A")
Set PutHereIfFound = .Find(What:=FindThis, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 5)
If Not PutHereIfFound Is Nothing Then
i = i + 1
Else
i = i
End If
End With
Loop While i <= 20
End Sub
可能有更好的方法来做到这一点。一旦你从'F'得到了值,你想做什么? –
好吧 - 把它们放到PutHereIfFound中,这是循环第一次迭代中的N5。 – speci
好的,如果我明白你想用Col F值填充COL N,如果找到匹配的话? –