PDA

View Full Version : Arrays in VBA errors



RusGrad
06-10-2020, 12:29 PM
Colleagues, I wrote a function in VBA to calculate the values of a two-dimensional array.
The principle of operation (I want to) is as follows:
1. The values entered were read and written in a single array;
2. Based on the elements of this array, the values of the two-dimensional array were calculated. In this case, the formula for the first column of the two-dimensional column and the rest column is different.
3. The output of the results on the tab.

Alas, the function does not work without any error detailing.

Please tell me where you can look to find the error.

Thanks for your support"

Paul_Hossler
06-10-2020, 04:54 PM
Your output example is not a 2-D array, so this is only the first part

I made it an array entered user defined function

It does return your expected numbers



Option Base 1
Option Explicit


Function C_k1(input0 As Range) As Variant
Dim input1 As Variant, output_1() As Double
Dim i As Long, N As Long

With input0

N = .Rows.Count

ReDim output_1(1 To N)

For i = 1 To N - 1
output_1(i) = Round(Exp(.Cells(i + 1, 1).Value / .Cells(i, 1).Value * 3), 5) ' =ROUND(EXP(S8/S7*3),5)
Next i

output_1(N) = Round(Exp(.Cells(2, 1).Value / .Cells(1, 1).Value * 3 * output_1(N - 1)), 5) ' =ROUND(EXP(S8/S7*3*U8),5)

End With

C_k1 = Application.WorksheetFunction.Transpose(output_1)

End Function

snb
06-11-2020, 02:47 AM
Function F_snb(st)
sn = st
sp = sn

sn(1, 1) = Round(Exp(sn(2, 1) / sn(1, 1) * 3), 5)
sn(2, 1) = Round(Exp(sn(3, 1) / sp(2, 1) * 3), 5)

For J = 3 To UBound(sn)
sn(J, 1) = Round(Exp(sp(J - 1, 1) / sp(J - 2, 1) * 3 * sn(J - 1, 1)), 5)
Next

F_snb = sn
End Function

In W7:W9, Arrayformula =F_snb(S7:S9) (ctrl-shift-enter)