Help with array loop

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I have a formula that I'm trying to loop through an array in cell B4, E4, and H4.

I haven't tried doing this with an FormulaArray before, and I think it might be throwing me off.

This is what I have:

VBA Code:
Dim i As Long
Dim rng As Range
Dim var As Variant

var = Array("""SA""", """SV""", """PA""")
Set rng = Range("B4")

For i = 0 To 6

rng.Offset(0, i + 3).FormulaArray = "=SUM(--(MMULT(--IF((Employees!$C$4:$C$3000=RIGHT(A4,3))*(Employees!$H$4:$H$3000=" & dept & "),(Employees!$K$4:$N$3000=""N""),0),TRANSPOSE(COLUMN(Employees!$K$4:$N$3000)^0))>0))"

Next i

I'm getting "Unable to set the FormulaArray property of the Range class".
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
What is dept meant to be?
 
Upvote 0
Are you trying to put the formula into B4 with SA, E4 with SV etc?
 
Upvote 0
Ok, try it like
VBA Code:
Dim i As Long
Dim rng As Range
Dim var As Variant

var = Array("""SA""", """SV""", """PA""")
Set rng = Range("B4")

For i = 0 To UBound(var)

rng.Offset(0, i * 3).FormulaArray = "=SUM(--(MMULT(--IF((Employees!$C$4:$C$3000=RIGHT(A4,3))*(Employees!$H$4:$H$3000=" & var(i) & "),(Employees!$K$4:$N$3000=""N""),0),TRANSPOSE(COLUMN(Employees!$K$4:$N$3000)^0))>0))"

Next i
 
Upvote 0
Ok, try it like
VBA Code:
Dim i As Long
Dim rng As Range
Dim var As Variant

var = Array("""SA""", """SV""", """PA""")
Set rng = Range("B4")

For i = 0 To UBound(var)

rng.Offset(0, i * 3).FormulaArray = "=SUM(--(MMULT(--IF((Employees!$C$4:$C$3000=RIGHT(A4,3))*(Employees!$H$4:$H$3000=" & var(i) & "),(Employees!$K$4:$N$3000=""N""),0),TRANSPOSE(COLUMN(Employees!$K$4:$N$3000)^0))>0))"

Next i
Of course that worked. Can I ask, why the difference with rng.Offset(0, i * 3) vs rng.Offset(0, i + 3)? In my mind I saw B4 + 3 columns, I don't understand the use of "*".

Thank you
 
Upvote 0
When using + you are adding 3 to i, so the formula will go into E4, F4 & G4, but using * multiplies i by 3 so the formula goes into B4, E4 & H4
 
Upvote 0

Forum statistics

Threads
1,215,107
Messages
6,123,126
Members
449,097
Latest member
mlckr

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