Scroll through column from top to bottom and replace 0's with value from cell above

后端 未结 1 1946
我寻月下人不归
我寻月下人不归 2021-01-28 01:19

I have to scroll through a column (actually two, but if I can get one going I can manage) from top to bottom and replace the 0\'s or blanks with cell value above it

相关标签:
1条回答
  • 2021-01-28 01:46

    So close:

    Sub ReplaceZeros()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim lr As Long
    Dim i As Long
    Dim ws As Worksheet
    Set ws = ActiveSheet
    With ws
        lr = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = 2 To lr
            If .Cells(i, "A").Value = 0 Then .Cells(i, "A").Value = .Cells(i - 1, "A").Value
        Next i
    End With
    
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    End Sub
    

    Note: The loop you had started at the bottom and went up, this would be fine but if you had two '0' in a row the second would remain zero, plus the step 7000 made the loop skip 7000 rows at a time.

    Then if all you want is the value above using Cells(i-1,"A").value will return the value of the cells directly above.

    0 讨论(0)
提交回复
热议问题