lundi 15 juin 2020

VBA If elseif statement to insert value in another range

I have this data to group a range of yield, if it's exist certain value. I write the coding below, but it's only return value elseif, other cell is empty if less than 60%. Can help to advise what is wrong with the code below? Attached is the range of data and result at column M, after run the code. which empty cell is supposed to fill up with value, but it's not.

    Sub FillIF()

Dim w2 As Worksheet
Dim lastrow1 As Long
Dim lastN As Long

Application.ScreenUpdating = False
Set w2 = Sheets("DataCompile")

On Error Resume Next
lastrow1 = w2.Cells(Cells.Rows.Count, "A").End(xlUp).Row
lastN = Range("N" & lastrow1).End(xlUp).Row + 1
For Each cell In w2.Range("M" & lastN & ":M" & lastrow1)
 If cell.Value > 0 And cell.Value <= 0.3 Then
    cell.Offset(0, 1).Value = "=<30% Yield"
 ElseIf cell.Value > 0.3 And cell.Value <= 0.6 Then
    cell.Offset(0, 1).Value = "=<60% Yield"
 Else
    cell.Offset(0, 1).Value = "60%><=100% Yield"
 End If
Next
On Error GoTo 0
Application.ScreenUpdating = True

End Sub

enter image description here

Aucun commentaire:

Enregistrer un commentaire