Consulting

Results 1 to 3 of 3

Thread: Arrays in VBA errors

  1. #1
    VBAX Newbie
    Joined
    Jun 2020
    Posts
    1
    Location

    Arrays in VBA errors

    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"
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •