+ Reply to Thread
Results 1 to 4 of 4

Store value from an array in new variable

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-04-2013
    Location
    Europe
    MS-Off Ver
    Office 365 ProPlus
    Posts
    147

    Store value from an array in new variable

    Can someone explain why this piece of code works

    Sub Macro1()
        Dim myVector(2) As Integer
        myVector(0) = 12
        myVector(1) = 41
        myVector(2) = 3
        Call macro2(myVector)
    End Sub
    
    Private Sub macro2(ByRef myVector() As Integer)
        Range("C1").Value = myVector(2)
    End Sub
    But this doesnt:

    Sub Macro1()
        Dim myVector(2) As Integer
        myVector(0) = 12
        myVector(1) = 41
        myVector(2) = 3
        Call macro2(myVector)
    End Sub
    
    Private Sub macro2(ByRef myVector() As Integer)
        Dim val As Integer
        Set val = myVector(2)
        Range("C1").Value = val
    End Sub
    I can't figure out how to store a value from the array in a new integer variable!

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Store value from an array in new variable

    Private Sub macro2(ByRef myVector() As Ingeter)
        Dim val As Integer
        val = myVector(2)
        Range("C1").value = val
    End Sub
    You only set objects, an integer isn't an object, also you shouldn't really use function names as variable names, it makes excel confused val is a function
    Last edited by Kyle123; 06-19-2013 at 04:52 AM.

  3. #3
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Store value from an array in new variable

    You are trying to set Val as object because SET used to assign objects. Just remove the Set and try

    val = myVector(2)


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  4. #4
    Forum Contributor
    Join Date
    03-04-2013
    Location
    Europe
    MS-Off Ver
    Office 365 ProPlus
    Posts
    147

    Re: Store value from an array in new variable

    Thanks guys!

+ 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