VBA If...Else If unable to return value to cells met condition

Ann Ooi

New Member
Joined
Jun 12, 2020
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hi, I have the data sheet "DataCompile", which I tried to use if...else if to return value into column M, if the criteria in column N is met. However, no matter how I change the coding, it will not return other value, and only return the largest value, which is "60%><=100% Yield".
What should I do to rectify the coding below, so that it can populate every cell with correct value, that is meeting the criteria?

VBA Code:
Sub FillIF()

Application.ScreenUpdating = False

Dim w2 As Worksheet
Dim LastRow As Long
Dim StartRow As Long

Set w2 = Sheets("DataCompile")

On Error Resume Next

LastRow = w2.Cells(w2.Rows.Count, 1).End(xlUp).Row
StartRow = w2.Cells(w2.Rows.Count, 14).End(xlUp).Row + 1

Dim i As Long
Dim Yield As Long
Dim Result As String

For i = StartRow To LastRow
Yield = w2.Range("M" & i).Value
      
If Yield > 0.6 And Yield <= 1 Then
    Result = "60%><=100% Yield"
ElseIf Yield > 0.3 And Yield <= 0.6 Then
    Result = "=<60% Yield"
Else
    Result = "=<30% Yield"
End If
  w2.Range("N" & i).Value = Result
 
Next

Application.ScreenUpdating = True

End Sub
 

Attachments

  • Capture3.jpg
    Capture3.jpg
    68.8 KB · Views: 11
Last edited:
Result = "'60%><=100% Yield"
Hi eduzs, finally, it works. It's real challenge going through all these coding. It kills lots of my cells, just to solve one minor dot.

Hi all, Thanks a lot. you are great. it works now!
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Not quite, it needs to be inside the double quotes to that is evaluated as a text comment when it is passed to the sheet rather than being a comment in vba.

I've added it in red below to try and make it stand out a little better. You shouldn't need it in this one, although it will do no harm, but the other 2 definitely need it.
Rich (BB code):
Result = "'60%><=100% Yield"
Hi Jason, yes, finally sorted out the issue, and thank you so much.
 
Upvote 0
Place a breakpoint at this line
"If Yield > 0.6 And Yield <= 1 Then"
run the code for that row and see what's "Yield" value.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top