将包含“#VALUE!”的单元格replace为周围单元格的平均值

我有一些包含#VALUE!单元格的数据列#VALUE! 。 令人讨厌的课程,所以我想写一个macros将取代这些#VALUE! 每个工作表中的单元格的平均值为2个单元格的上方和下方。

例如。

  AA 1 1 1 2 2 2 3 #VALUE! -> 3 4 4 4 5 5 5 

我试图按照这篇文章和这篇文章,并有以下代码,但作为一个初学者,我显然缺less的东西,因为我收到“运行时错误13”。

 Sub Test() Dim wks As Worksheet For Each wks In ThisWorkbook.Worksheets With wks Dim Qty As Range For Each Qty In Range("A:ZZ").Cells If InStr(1, (Qty.Value), "#VALUE!") Then ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C:R[-1]C,R[1]C:R[2]C)" Range("A:ZZ").Select End If Next End With Next End Sub 

任何帮助是极大的赞赏。

考虑:

 If InStr(1, (Qty.Text), "#VALUE!") Then 

并且由于Qty是一个范围 ,所以不要使用ActiveCell; 代替:

 Qty.FormulaR1C1 = "=AVERAGE(R[-2]C:R[-1]C,R[1]C:R[2]C)" 

编辑#2:

这是一些工作代码:

 Sub Test() Dim wks As Worksheet, Qty As Range For Each wks In ThisWorkbook.Worksheets With wks For Each Qty In Intersect(.UsedRange, .Range("A:ZZ").Cells) If InStr(1, (Qty.Text), "#VALUE!") Then Qty.FormulaR1C1 = "=AVERAGE(R[-2]C:R[-1]C,R[1]C:R[2]C)" End If Next End With Next wks End Sub