IF statement in VBA

wrightyrx7

Well-known Member
Joined
Sep 15, 2011
Messages
994
HI all,

I have the following code but do not know how to put the formula in:-

The formula i want to use is:
Code:
=IF(A3=A2,IF(B3=B2,G2+1,1),1)

my attempt at putting it into VBA is:
Code:
Sub test()    Dim lastRow As Integer
    Dim rngSeq As Range
    Dim i As Range
    
    lastRow = Range("A3").End(xlDown).Row


    With ActiveSheet
        Set rngSeq = Range("M3:M" & lastRow)


        
            For Each i In rngSeq.Cells
                i.Formula = "=if(A" & .Row = A" & .Row - 1,
                


            Next i
    End With


End Sub

Please help
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,

Perhaps try it something like this:

Code:
Sub test()
    
    Dim lastRow As Long
    
    With ActiveSheet
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range("M3:M" & lastRow).Formula = _
            "=IF(A3 = A2, IF(B3 = B2, G2 + 1, 1), 1)"
    End With

End Sub
 
Upvote 0
Try
Code:
Sub test()
Dim lastRow As Long, rngSeq As Range
lastRow = Range("A3").End(xlDown).Row
Set rngSeq = Range("M3:M" & lastRow)
rngSeq.Formula = "=IF(A3=A2,IF(B3=B2,G2+1,1),1)"
End Sub
 
Upvote 0
Some suggestions:
Code:
Sub test2()
Dim lRow As Long, i As Long
Application.ScreenUpdating = False    
    With ActiveSheet
        lRow = .Range("A" & Rows.Count).End(xlUp).row
        For i = 3 To lRow
            .Range("M" & i).Formula = "=IF(A" & i & "=A" & i - 1 & ",IF(B" & i & "=B" & i - 1 & ",G" & i & "+1,1),1)"
        Next i
    End With
Application.ScreenUpdating = True
End Sub
And
Code:
Sub test3()
    Dim lRow As Long
        
    Application.ScreenUpdating = False
        
    With ActiveSheet
        lRow = .Range("A" & Rows.Count).End(xlUp).row
        .Range("M3").Formula = "=IF(AND(A3=A2,B3=B2),G2+1,1)"
        .Range("M3:M" & lRow).FillDown
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Fantastic!! thank you both.

Tried them both for feedback purposes and both work great. Thank you again.



Can any of you help to input the following FormulaArray into column "N" in the same way as the previous.

Code:
=INDEX(Allowances!D$1:D$198,MATCH(1,(Allowances!C$1:C$198=G3)*(Allowances!E$2:E$198=B3),0))

I tried to input :

Code:
Sub test()    
    Dim lastRow As Long
    
    With ActiveSheet
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        .Range("N3:N" & lastRow).FormulaArray = _
            "=INDEX(Allowances!D$1:D$198,MATCH(1,(Allowances!C$1:C$198=G2)*(Allowances!E$2:E$198=B3),0))"
    End With


End Sub

But the G2 and B3 remain static and do not change.

Regards
Chris
 
Upvote 0
For entering ARRAY formula in VBA, you have to follow Jacks 2nd example.
Enter the array formula in the first cell only, then fill down.
 
Upvote 0
Thanks Jonmo1 the array formula is now working.

Now for one last one if you can help because i keep getting an error message

Code:
=IF(I3="N","",IFERROR(VLOOKUP(A3,'Active TP'!$A$2:$E$1976,5,FALSE),""))

I think when i put it into VBA i need some more "" in it, but not 100%

Thanks again
 
Upvote 0
In addition to Jacks 2nd example, there are a few other methods you can try. For example:

Code:
Sub test()

    Dim lastRow As Long
    
    With ActiveSheet
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        With .Range("N3:N" & lastRow)
            .Formula = "=INDEX(Allowances!D$1:D$198,MATCH(1,(Allowances!C$1:C$198=G2)*(Allowances!E$2:E$198=B3),0))"
            .FormulaArray = .FormulaR1C1
        End With
    End With 

End Sub
This article from Colin Legg provides a detailed description of all your options:
Working With Range.FormulaArray In VBA | RAD Excel
 
Upvote 0
Yep, when entering a formula through VBA, and that formula contains quote marks, you have to double up the quote marks.

Try
=IF(I3=""N"","""",IFERROR(VLOOKUP(A3,'Active TP'!$A$2:$E$1976,5,FALSE),""""))
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,375
Members
448,955
Latest member
BatCoder

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