Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA: "For" and "If" statement on a single line?

Is it somehow possible in VBA to put a For and If statement in a single line? The closest what I could get is this:

For i = 0 To n: If a = i Then a = b
Next i

If I write:

For i = 0 To n: If a = i Then a = b: Next i

I get the "Next without For" error.

like image 658
Klaidonis Avatar asked Oct 14 '25 21:10

Klaidonis


1 Answers

You can do it with IIf, rather than If.

This code won't throw the error:

For i = 0 To n: IIf a = i, a = b, False: Next i
like image 52
Robin Mackenzie Avatar answered Oct 17 '25 17:10

Robin Mackenzie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!