How to pass an Array to AngularJS?

Pass array to function

  • I was trying to create a sub and function to perform subtraction on an array. I got the Sub working but the function is not 100%. The function appears to be receiving the array and performing subtraction but not returning the results to the test sub procedure. Function Num(ByVal rng As Variant) As Variant 'trying to convert this function from the Sub RangeToVariant Dim r As Long, c As Integer For r = 1 To UBound(rng, 1) - 1 For c = 1 To UBound(rng, 2) rng(r + 1, c) = rng(r, c) - rng(r + 1, c) Next c Next r 'Uncommenting the below statement will produce the correct answer but 'I think this statement needs to be in the Sub Testing 'Range("c1:c20").Value = rng End Function Sub testing() 'Sub to test Function Num rng = Range("A1:A20").Value Num (rng) 'the below statement produces the wrong or original values. Range("c1:c20").Value = rng End Sub Sub RangeToVariant2() 'This works fine on its own. Basically subtracts A1 - A2 - A3... Dim x As Variant Dim r As Long, c As Integer ' Read the data into the variant x = Range("A1:A20").Value ' Loop through the variant ar For r = 1 To UBound(x, 1) - 1 For c = 1 To UBound(x, 2) 'subtraction the numbers x(r + 1, c) = x(r, c) - x(r + 1, c) Next c Next r ' Transfer the variant back to the sheet Range("C1:C20") = x End Sub

  • Answer:

    Michael, this produces the same result as your stand alone function and is how I would do it: Function Num(ByVal rng As Variant) As Variant 'trying to convert this function from the Sub RangeToVariant Dim r As Long, c As Integer For r = 1 To UBound(rng, 1) - 1 For c = 1 To UBound(rng, 2) rng(r + 1, c) = rng(r, c) - rng(r + 1, c) Next c Next r 'Uncommenting the below statement will produce the correct answer but 'I think this statement needs to be in the Sub Testing Num = rng End Function Sub testing() 'Sub to test Function Num rng = Range("A1:A20").Value 'the below statement produces the expected result. Range("c1:c20").Value = Num(rng) End Sub so you assign the result to the function name and use that to get the result.

Miningco.com Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.