I would do it this way:
Sub duh()
Dim start As Range: Set start = Range("o1")
Dim r As Range
Dim doCopy As Boolean: doCopy = False
For Each r In Range(start, start.End(xlDown))
If r.Value <> 0 Then
If r.Row <> start.Row Then
Range(start, r.Offset(-1)).Copy
doCopy = True
End If
Exit For
End If
Next
If doCopy Then ActiveSheet.Paste Range("a1")
End Sub
First, we define start
as the cell to start at. r
will represent the cell that we are evaluating. doCopy
will be set to true if the paste will be done (false if we have nothing to copy).
The For Each r...
loop will iterate through every cell from start
through the bottom of the column (this may not be exactly what you want....)
When we find a non-zero value, first check to see if we're still on the first row. If we are, then we'll just exit the loop. If we're not on the first row, then Range(start, r.Offset(-1)).Copy
will copy to the clipboard the range starting at start
and ending at the cell above r
; set doCopy
to True and exit the loop. Finally, do the paste only if doCopy
is True.