Question/help with VBA spilling.

BEDE

New Member
Joined
Mar 29, 2024
Messages
25
Office Version
  1. 365
Platform
  1. Windows
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.

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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Since no replies maybe I'm asking the wrong question. What do you do to get your vba to spill? Does anyone have any actual code they use for spilling? Just for comparison.
 
Upvote 0

Forum statistics

Threads
1,225,117
Messages
6,182,930
Members
453,140
Latest member
SAbboushi

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