Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

VBA noob

  • 24-04-2010 02:48PM
    #1
    Closed Accounts Posts: 1,581 ✭✭✭


    Hi guys,

    Im using vba for large iterations in excel but Im falling at the first hurdle.

    I want an array of the form u = [u1,u2,u3.....u21]

    I have entered boundary conditions and initial vales for the interior nodes.

    Ive named an output cell 'u' in the spreadsheet.

    Sub Test()

    Dim u() As Double

    ReDim u(1 To 21)


    u(1) = 0
    u(21) = 1

    For j = 2 To 20
    u(j) = 0.5
    Next j

    Range("u").Activate
    ActiveCell = u()


    End Sub


    Using the above, only a single value is returned, not the entire range.

    Im aware its probably an embarrassingly easy problem to solve, but Im having no joy.

    Thanks.


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    My VBA is years out of date, but the content of cell "u" needs to be all the elements in the array u, is that correct? If so you could loop through the elements and add them to a string, end then display the string.

    I can't remember if there's a foreach loop in VB but you'll get the idea from the code below, I'm creating a comma delimited string for the content of the cell.
    Sub Test()
    
    Dim u() As Double
    
    ReDim u(1 To 21)
    
    
    u(1) = 0
    u(21) = 1
    
    For j = 2 To 20
    u(j) = 0.5
    Next j
    
    dim sContent as String
    [b]foreach d as Double in u
        sContent = sContent + "," + str(d)
    next d[/b]
    
    Range("u").Activate
    [B]ActiveCell = sContent[/B]
    
    
    End Sub 
    


Advertisement