lundi 27 novembre 2017

Cell Address in a loop

I am looping through a range of cells to check if a value is 0. My logic is if cell value is zero, then cell value is the previous cells value. If that previous cell is also zero, then it is the next cell's value. But I said what if the last cell or first cell is zero? I need to check that too because if it is the first cell or last, then the loop fails. My question is, how do I find the last value within the range because it is dynamic. I know the column, but not the row number. The data starts at row 2 and then goes to row X.

For each Cell In RANGE
    If Cell.Address="A2" Then
        If Cell.Value=0 Then
           Cell.Value=Cell.Offset(1,0).Value
        End if

     Elseif Cell.Address="AX" Then 'X is the last row
        If Cell.Value=0 Then
           Cell.Value=Cell.Offset(-1,0).Value
        End If

    Elseif Cell.Value=0 and Cell.Offset(1,0).Value=0 Then 
        Cell.Value=Cell.Offset(-1,0).Value

    Elseif Cell.Value=0 Then
        Cell.Value=Cell.Offset(1,0).Value

    Else
        Do Nothing

    End If
Next

Aucun commentaire:

Enregistrer un commentaire