My VBA is limited. Through experience I can identify equations and syntax but I'm missing details.
Long story short. AI gave me the code below. Surprisingly it works. My concern is that, from what I can tell, it sets everything as an array, then pipes it for count it, finds the biggest row and then loops it(is that a double loop?). Is this really what Microsoft had in mind or is there a better more efficient way.
Long story short. AI gave me the code below. Surprisingly it works. My concern is that, from what I can tell, it sets everything as an array, then pipes it for count it, finds the biggest row and then loops it(is that a double loop?). Is this really what Microsoft had in mind or is there a better more efficient way.
VBA Code:
Function FINDNEWPT(A As Range, B As Range, C As Range, D As Range, E As Range, MATCHTHIS As Range) As Variant
Dim H As Double, L As Double
Dim results() As Variant
Dim i As Long, maxRows As Long
' Determine the maximum number of rows from all input ranges
maxRows = Application.WorksheetFunction.Max(A.Rows.Count, B.Rows.Count, C.Rows.Count, D.Rows.Count, E.Rows.Count, MATCHTHIS.Rows.Count)
ReDim results(1 To maxRows)
For i = 1 To maxRows
' Check if the current row exists for each parameter
If i <= A.Rows.Count And i <= B.Rows.Count And i <= C.Rows.Count And i <= D.Rows.Count And i <= E.Rows.Count And i <= MATCHTHIS.Rows.Count Then
If IsNumeric(A.Cells(i, 1)) And IsNumeric(B.Cells(i, 1)) And IsNumeric(C.Cells(i, 1)) And
IsNumeric(D.Cells(i, 1)) And IsNumeric(E.Cells(i, 1)) AND IsNumeric(MATCHTHIS.Cells(i, 1)) Then
H = 5
L = 0
Do While (H - L) > 0.00000001
Dim NEWTON As Double
NEWTON = (H + L) / 2
If FMDM(A.Cells(i, 1), B.Cells(i, 1), C.Cells(i, 1), D.Cells(i, 1), NEWTON, E.Cells(i, 1)) > MATCHTHIS.Cells(i, 1) Then
H = NEWTON
Else
L = NEWTON
End If
Loop
results(i) = (H + L) / 2
Else
results(i) = CVErr(xlErrValue) ' Return #VALUE! if any parameter is not numeric
End If
Else
results(i) = CVErr(xlErrNA) ' Return #N/A if the row does not exist for any parameter
End If
Next i
FINDNEWPT = Application.Transpose(results) ' Transpose to return as a vertical array
End Function