+ Reply to Thread
Results 1 to 3 of 3

VBA Help: To store results in Array.. Myarray1,myarray2,myarray3 etc...

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-22-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    102

    VBA Help: To store results in Array.. Myarray1,myarray2,myarray3 etc...

    Given an example below.. would need to store results in a "array" .. Line 4 says "type mismatch"


    Sub arraytest()
    Dim myarray As Variant
    For i = 1 To 10
    myarray(i) = i
    Next
    For i = 1 To 10
    Range("B" & i).Value = myarray(i)
    Next
    End Sub
    Thanks.
    Last edited by vidyuthrajesh; 02-29-2012 at 04:19 PM. Reason: Yes.. it works !! Thanks

  2. #2
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: VBA Help: To store results in Array.. Myarray1,myarray2,myarray3 etc...

    try this way
    Sub arraytest()
    Dim i As Long
    Dim myarray As Variant
    
    ReDim myarray(1 To 10)
    For i = 1 To 10
    myarray(i) = i
    Next
    For i = 1 To 10
    Range("B" & i).Value = myarray(i)
    Next
    End Sub
    or without second loop
    Sub arraytest()
    Dim i As Long
    Dim myarray As Variant
    
    ReDim myarray(1 To 10)
    For i = 1 To 10
    myarray(i) = i
    Next
    Range("B1").Resize(10).Value = Application.Transpose(myarray)
    End Sub
    Last edited by tom1977; 02-29-2012 at 04:02 PM.
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: VBA Help: To store results in Array.. Myarray1,myarray2,myarray3 etc...

    Sub arraytest()
    Dim myarray(1 To 10) As Variant
    For i = 1 To 10
    myarray(i) = i
    Next
    For i = 1 To 10
    Range("B" & i).Value = myarray(i)
    Next
    End Sub
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1