Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert alternating formulas into the same column

Tags:

excel

vba

Is it possible to use vba to insert 2 different formulas into the same column? I want the formulas to alternate. I'm trying to do Sumif's but the sum range keeps changing after every second line, so I need to be able to insert 2 different formulas.

So to try to explain this further - Say in Cell A1 I want to use a sumif and the sum range is column K, but then A2 has to sum column L, and then A3 has to go back to column K and so on.

I tried to use using a vlookup , but there is no unique identifier.

Is what I'm trying to achieve even possible?

like image 514
eenz Avatar asked Jan 25 '26 11:01

eenz


1 Answers

The trick is to use Step 2 in your loop.

You will need to adjust the loop range of course as this is just a generic example. If you drop this in a worksheet code space and run, you will notice every other row switches between summing Column K and Column L


Sub Alternate()

Dim i As Long

For i = 1 To 20 Step 2
    Range("A" & i).Formula = "=Sum(K:K)"
    Range("A" & i + 1).Formula = "=Sum(L:L)"
Next i


End Sub

Also, qualify your objects properly unlike the above example!

enter image description here

like image 171
urdearboy Avatar answered Jan 28 '26 03:01

urdearboy



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!