Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VBA noob

Options
  • 24-04-2010 2: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 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