Problem with VBA, inputs and loops not working

%3CLINGO-SUB%20id%3D%22lingo-sub-1479661%22%20slang%3D%22en-US%22%3EProblem%20with%20VBA%2C%20inputs%20and%20loops%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1479661%22%20slang%3D%22en-US%22%3E%3CP%3ENothing%20works%20with%20my%20code%20anymore.%20If%20i%20try%20to%20solve%20the%20first%20problem%20more%20promblems%20occour%20further%20down.%26nbsp%3B%20I%20which%20to%20do%20three%20things%3A%201)%20determine%20the%20length%20of%20my%20input%20(number%20of%20cells)%202)%20A%20for-loop%20based%20on%20the%20length%20of%20input%203)%20find%20the%20average%20from%20the%20loop.%20Here%20is%20my%20code%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EFunction%20TILLV(rng%20As%20Range)%20As%20Double%0A%0AL%20%3D%20(UBound(rng%2C%201)%20-%20LBound(rng%2C%201)%20%2B%201)%0ADim%20a()%20As%20Double%0AReDim%20a(L)%0ADim%20i%20As%20Integer%0A%0AFor%20i%20%3D%201%20To%20L%0A%20%20%20%20%20%20%20%20If%20rng(i)%20%26lt%3B%200%20And%20rng(i%20%2B%201)%20%26lt%3B%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20a(i)%20%3D%20rng(i)%20%2F%20(rng(i%20%2B%201))%0A%20%20%20%20%20%20%20%20ElseIf%20rng(i)%20%26gt%3B%200%20And%20rng(1%20%2B%201)%20%26gt%3B%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20a(i)%20%3D%20rng(i%20%2B%201)%20%2F%20rng(i)%0A%20%20%20%20%20%20%20%20ElseIf%20rng(i)%20%26lt%3B%200%20And%20rng(i%20%2B%201)%20%26gt%3B%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20a(i)%20%3D%20(rng(i%20%2B%201)%20-%20rng(i))%20%2F%20(-rng(i))%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20a(i)%20%3D%200%0A%20%20%20%20%20%20%20%20End%20If%0ANext%20i%0A%0Aaverage%0ATILLV%20%3D%20average%0A%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELike%20I%20said%2C%20nothing%20works.%20I%20can't%20even%20figure%20out%20how%20to%20find%20the%20lenght%20of%20the%20input%20data%20(which%20is%20just%20a%20row%20of%20cells).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1479661%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1487401%22%20slang%3D%22en-US%22%3ERe%3A%20Problem%20with%20VBA%2C%20inputs%20and%20loops%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1487401%22%20slang%3D%22en-US%22%3EHello%20there%2C%3CBR%20%2F%3E%3CBR%20%2F%3ECan%20you%20give%20an%20example%20of%20how%20you're%20using%20the%20formula%3F%20When%20you%20say%20'length%20of%20the%20input%20data'%2C%20can%20you%20please%20be%20more%20specific%20and%20tell%20us%20exactly%20what%20that%20means%3F%20It's%20confusing.%20I'm%20not%20sure%20if%20you%20mean%20the%20actual%20length%20of%20the%20Range%20reference%20or%20the%20number%20of%20cells%20in%20the%20Range.%20Give%20a%20few%20examples%20of%20how%20it%20would%20work.%20For%20the%20best%20results%2C%20give%20us%20the%20cell%20values%20being%20referenced%20along%20with%20the%20desired%20output.%3C%2FLINGO-BODY%3E
Highlighted
Visitor

Nothing works with my code anymore. If i try to solve the first problem more promblems occour further down.  I whish to do three things: 1) determine the length of my input (number of cells) 2) A for-loop based on the length of input 3) find the average from the loop. Here is my code

 

 

 

Function TILLV(rng As Range) As Double

L = (UBound(rng, 1) - LBound(rng, 1) + 1)
Dim a() As Double
ReDim a(L)
Dim i As Integer

For i = 1 To L
        If rng(i) < 0 And rng(i + 1) < 0 Then
            a(i) = rng(i) / (rng(i + 1))
        ElseIf rng(i) > 0 And rng(1 + 1) > 0 Then
            a(i) = rng(i + 1) / rng(i)
        ElseIf rng(i) < 0 And rng(i + 1) > 0 Then
            a(i) = (rng(i + 1) - rng(i)) / (-rng(i))
        Else
            a(i) = 0
        End If
Next i

average = ... 
TILLV = average

End Function

 

 

 

Like I said, nothing works. I can't even figure out how to find the lenght of the input data (which is just a row of cells). When I try to use UBound i only get a message saying that it 'needs to be a matrix'.

1 Reply
Highlighted
Hello there,

Can you give an example of how you're using the formula? When you say 'length of the input data', can you please be more specific and tell us exactly what that means? It's confusing. I'm not sure if you mean the actual length of the Range reference or the number of cells in the Range. Give a few examples of how it would work. For the best results, give us the cell values being referenced along with the desired output.