Excel VBA – For循环。find具有值的单元格

我正在努力编写一个有效的macros,它将find列中出现错误的单元格,然后用第一个非空单元格的值replace该单元格,并在其下面没有错误(可能会有连续的错误单元格),然后循环12列。

下面的代码replace了所有12列中的每个错误单元格,但不是一致的方式:一些单元格将确实由包含数字的下一个单元格填充,但是一些单元格的值将是第二个包含一个数字的单元格。 我不知道我的代码中的问题在哪里。

Option Explicit Sub ClearError() ThisWorkbook.Sheets("WorkSheet1").Activate Dim c, x, z As Integer Dim y As Long For z = 3 To 14 Step 1 ' Start with column 'C' and do for total of 12 columns x = 999 For c = 1 To x Step 1 If IsError(Cells(c, z)) Then Cells(c, z) = Range(Cells(1, z), Cells(x, z)).Find(y, _ After:=Cells(c, z), LookIn:=xlValues, SearchDirection:=xlNext).Value End If Next c Next z End Sub 

如果你能提供任何见解或build议,我将不胜感激。 感谢您的时间! 赌注

尝试颠倒你的循环。

 For c = x to 1 Step -1 If IsError(Cells(c, z)) Then Cells(c, z) = Cells(c + 1, z) End If Next c