Excel 查找方法vba运行时错误91
下面代码中的find方法存在问题Excel 查找方法vba运行时错误91,excel,vba,find,runtime-error,Excel,Vba,Find,Runtime Error,下面代码中的find方法存在问题 c = Cells.find(wValue).Address 我总是得到运行时错误91:对象变量或未设置块变量。 当我在一个没有循环的试用代码中尝试它时,它工作得非常好,但我真的不知道,为了使它工作,我到底需要修改什么 你对此有什么建议吗 Sub find() Dim cRange As Range, rngQty As Range, z As Range Dim Date1 As Integer Dim c As String With Applicat
c = Cells.find(wValue).Address
我总是得到运行时错误91:对象变量或未设置块变量。
当我在一个没有循环的试用代码中尝试它时,它工作得非常好,但我真的不知道,为了使它工作,我到底需要修改什么
你对此有什么建议吗
Sub find()
Dim cRange As Range, rngQty As Range, z As Range
Dim Date1 As Integer
Dim c As String
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wkbZ = Workbooks("Order History.xlsm")
Set wkbY = Workbooks("Forecast Form.xlsm")
For Each z In wkbZ.Sheets("2015").Range(Range("A2"), Range("A2").End(xlDown))
Set rngQty = z.Offset(, 3)
Date1 = Month(z.Offset(, 4))
wValue = z.Value
wkbY.Activate
c = Cells.find(wValue).Address
Set cRange = Range(c)
cRange.Select
If Date1 = 1 Then
Set rngPaste1 = Selection.Offset(, 3)
End If
If Date1 = 2 Then
Set rngPaste = cRange.Offset(, 4)
End If
If Date1 = 3 Then
Set rngPaste = cRange.Offset(, 5)
End If
If Date1 = 4 Then
Set rngPaste = cRange.Offset(, 6)
End If
If Date1 = 5 Then
Set rngPaste = cRange.Offset(, 7)
End If
If Date1 = 6 Then
Set rngPaste = cRange.Offset(, 8)
End If
If Date1 = 7 Then
Set rngPaste = cRange.Offset(, 9)
End If
If Date1 = 8 Then
Set rngPaste = cRange.Offset(, 10)
End If
If Date1 = 9 Then
Set rngPaste = cRange.Offset(, 11)
End If
If Date1 = 10 Then
Set rngPaste = cRange.Offset(, 12)
End If
If Date1 = 11 Then
Set rngPaste = cRange.Offset(, 13)
End If
If Date1 = 12 Then
Set rngPaste = cRangec.Offset(, 14)
End If
rngPaste.Value = (rngPaste.Value) + (rngQty.Value)
wkbZ.Activate
Next
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
未经测试,但应接近
Sub find()
Dim cRange As Range, rngQty As Range, z As Range
Dim Date1 As Integer
Dim rngSrch As Range
Dim wkbZ As Workbook
Dim wkbY As Workbook
Set wkbZ = Workbooks("Order History.xlsm")
Set wkbY = Workbooks("Forecast Form.xlsm")
'fully-qualify all Range calls with a worksheet object
With wkbZ.Sheets("2015")
Set rngSrch = .Range(.Range("A2"), .Range("A2").End(xlDown))
End With
For Each z In rngSrch.Cells
Set rngQty = z.Offset(0, 3)
Date1 = Month(z.Offset(0, 4))
'better to specify whether looking at full content or part..
'also not clear which sheet you're searching?
'notice no select/activate needed...
Set cRange = wkbY.Sheets(1).Cells.find(what:=z.Value, lookat:=xlWhole)
If Not cRange Is Nothing Then
With cRange.Offset(0, Date1 + 2)
.Value = .Value + rngQty.Value
End With
End If
Next
End Sub
您是在循环的第一次迭代中得到错误还是在第二次迭代中得到错误?z.value返回什么值?如果
Find()
无法匹配,那么它将返回Nothing
,因此您需要测试firstwkbZ.Sheets(“2015”)是否需要在运行该或for时成为活动工作表。。。下一步将失败。请参见Set rngpast=cRangec.Offset(,Date1+2)
如果块,则不需要所有这些!伙计,你是最棒的。它跑得很平稳。谢谢!