In Excel VBA, I have a function for y with input of 3 variables (a, b, c). What I would like to do is to use VBA to apply this function automatically to a range of cells (combinations of a, b and c as listed in the worksheet).
The way I have my data set up:
b 1 1 1 1 1 2 2 2 2 3 3 3 4 4 etc.
c 2 3 4 5 6 3 4 5 6 4 5 6 5 6 etc.
a
1 . . . . . . . . . . . . . .
2 . . . . . . . . . . . . . .
3 . . . . . . . . . . . . . .
4 . . . . . . . . . . . . . .
etc.
The 'dots' are where the results of function(a,b,c) should end up (e.g., top left dot should be result of "function(1,1,2)"). Of note, within a column the same paired values of b and c should be used (so within the same column, only 'a' varies while b and c remain constant).
A previous version of this function only had 2 variables as input (a and b, set up in the same way as the data above), and I used a 2D-array (values of 'a' on rows vertically, values of 'b' on columns horizontally) to apply the function on all combinations of a and b using the following code:
Sub applyfunction()
Dim ws As Worksheet
Dim arr_ab()
Dim a, b, i As Long, j As Long
For Each ws In Worksheets
If ws.Name Like "Util*" Then
With ws
a = .Range("B5:B244").Value
b = .Range("C2:CG2").Value
ReDim arr_ab(1 To UBound(a), 1 To UBound(b, 2))
For i = LBound(arr_ab) To UBound(arr_ab)
For j = LBound(arr_ab, 2) To UBound(arr_ab, 2)
arr_ab(a, b) = "=function(" & a(i, 1) & ", " & b(1, j) & ")"
Next j
Next i
.Range("C5:CG244").Value = arr_ab()
End With
End If
Next ws
End Sub
However, now that I have added a third variable (c) to the function, I want to use function(a,b,c). I have some trouble getting it to work like I did when I only had 2 variables. Is there a way to still use an array for this?
Thanks in advance for any help.
Edit: I want to use the values (not the references) of a,b,c in the function