if then else question

El_Danzo

New Member
Joined
Oct 30, 2018
Messages
1
Hi, everything I used to know about VBA I have forgotten, so this simple task is causing me a headache.
I want to populate the value of B3 incrementally from the cell right above it IF the value of A3 is the same as A2. ELSE populate as .001 and repeat for B4,
and loop until a blank cell is reached.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi

You didn't state what you wanted to increase by so I used 1

Code:
Sub IncreaseB()
LastRow = Range("A" & Rows.Count).End(xlUp).Row

For x = 3 To LastRow

    If Cells(x, "A").Value = "" Then Exit For

    If Cells(x, "A").Value = Cells(x - 1, "A").Value Then
        Cells(x, "B").Value = Cells(x - 1, "B").Value + 1
    Else
        Cells(x, "B").Value = "0.01"
    End If
Next x

End Sub

You could also use a formula;

Code:
=IF(A3=A2,B2+1,0.01)
 
Upvote 0
Code:
Sub IncreaseB()
With Range([B3], Cells(Rows.Count, "A").End(xlUp)(1, 2))
    .Formula = "=IF(A3=A2,B2+1,0.01)"
    .Value = .Value
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,447
Latest member
M V Arun

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