VBA: Multiple if conditions with loop

Abdulhaq

New Member
Joined
Feb 1, 2019
Messages
9
Dear All,

I am writing a macro where i have multiple conditions for getting the data and i am stuck here as it is giving only single output that is "0-1 days"... Please help

below is my code :

Lastrow1 = Sheets("Rawdata").Range("A" & Rows.Count).End(xlUp).Row

For A = 2 To Lastrow1


If Cells(A, "D") >= 0 Then
Cells(A, "CK") = "0-1 Days"
ElseIf Cells(A, "D") >= 1 Then
Cells(A, "CK") = "1-3 Days"
ElseIf Cells(A, "D") >= 3 Then
Cells(A, "CK") = "3-5 Days"
ElseIf Cells(A, "D") >= 5 Then
Cells(A, "CK") = "5-10 Days"
ElseIf Cells(A, "D") >= 10 Then
Cells(A, "CK") = "10-20 Days"
ElseIf Cells(A, "D") >= 20 Then
Cells(A, "CK") = "20-30 Days"
ElseIf Cells(A, "D") >= 30 Then
Cells(A, "CK") = "30-40 Days"
ElseIf Cells(A, "D") >= 45 Then
Cells(A, "CK") = "45-60 Days"
ElseIf Cells(A, "D") >= 60 Then
Cells(A, "CK") = "60-90 Days"
ElseIf Cells(A, "D") >= 90 Then
Cells(A, "CK") = "90+ Days"
Else: Cells(A, "D") = "overdue"
End If

Next

End With
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You are getting "0-1 Days" because the IF statement will look at the first value and say "if the cell is greater or equal to 0 then assign me as "0-1 Days" - you should try to invert the equality sign.
So If Cells(A, "D") < 1 Then
Cells(A, "CK") = "0-1 Days"
ElseIf ...
Keep using < instead of >=
 
Upvote 0
You can also use a select case like
Code:
   For a = 2 To LastRow
      Select Case Cells(a, "D")
         Case Is < 1
            Cells(a, "CK") = "0-1 Days"
         Case Is < 3
            Cells(a, "CK") = "1-3 Days"
         Case Is < 5
            Cells(a, "CK") = "3-5 Days"
      End Select
   Next a
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,521
Members
449,088
Latest member
RandomExceller01

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