+ Reply to Thread
Results 1 to 7 of 7

How to transfer selected cells' values into an array in VBA?

  1. #1
    Julian
    Guest

    How to transfer selected cells' values into an array in VBA?

    For example, If A1:K11 is selected, then the Array in VBA should be:

    A(1,1) = A1, A(1,2) = B1 A(1,3) = C1 ...
    A(2,1) = A2, A(2,2) = B2 A(2,3) = C2 ...
    ....

    Thanks!



  2. #2
    Dave Peterson
    Guest

    Re: How to transfer selected cells' values into an array in VBA?

    Option Explicit
    sub testme
    dim myArr as variant
    myArr = activesheet.range("a1:K11").value
    end sub

    Julian wrote:
    >
    > For example, If A1:K11 is selected, then the Array in VBA should be:
    >
    > A(1,1) = A1, A(1,2) = B1 A(1,3) = C1 ...
    > A(2,1) = A2, A(2,2) = B2 A(2,3) = C2 ...
    > ...
    >
    > Thanks!


    --

    Dave Peterson

  3. #3
    Julian
    Guest

    Re: How to transfer selected cells' values into an array in VBA?

    Thanks...
    But what should I do if the selection is flexible?


    "Dave Peterson" <[email protected]> wrote in message
    news:[email protected]...
    > Option Explicit
    > sub testme
    > dim myArr as variant
    > myArr = activesheet.range("a1:K11").value
    > end sub
    >
    > Julian wrote:
    >>
    >> For example, If A1:K11 is selected, then the Array in VBA should be:
    >>
    >> A(1,1) = A1, A(1,2) = B1 A(1,3) = C1 ...
    >> A(2,1) = A2, A(2,2) = B2 A(2,3) = C2 ...
    >> ...
    >>
    >> Thanks!

    >
    > --
    >
    > Dave Peterson




  4. #4
    Dave Peterson
    Guest

    Re: How to transfer selected cells' values into an array in VBA?

    myarr = selection.value

    I'm not sure what flexible means???

    Julian wrote:
    >
    > Thanks...
    > But what should I do if the selection is flexible?
    >
    > "Dave Peterson" <[email protected]> wrote in message
    > news:[email protected]...
    > > Option Explicit
    > > sub testme
    > > dim myArr as variant
    > > myArr = activesheet.range("a1:K11").value
    > > end sub
    > >
    > > Julian wrote:
    > >>
    > >> For example, If A1:K11 is selected, then the Array in VBA should be:
    > >>
    > >> A(1,1) = A1, A(1,2) = B1 A(1,3) = C1 ...
    > >> A(2,1) = A2, A(2,2) = B2 A(2,3) = C2 ...
    > >> ...
    > >>
    > >> Thanks!

    > >
    > > --
    > >
    > > Dave Peterson


    --

    Dave Peterson

  5. #5
    Julian
    Guest

    Re: How to transfer selected cells' values into an array in VBA?

    I mean can I create a Module like this:

    Public Function Testing(myInput As Variant)
    Dim myArr As Variant
    myArr = ActiveSheet.Range(myInput).Value
    End Function

    Thanks again!

    "Dave Peterson" <[email protected]> wrote in message
    news:[email protected]...
    > myarr = selection.value
    >
    > I'm not sure what flexible means???
    >
    > Julian wrote:
    >>
    >> Thanks...
    >> But what should I do if the selection is flexible?
    >>
    >> "Dave Peterson" <[email protected]> wrote in message
    >> news:[email protected]...
    >> > Option Explicit
    >> > sub testme
    >> > dim myArr as variant
    >> > myArr = activesheet.range("a1:K11").value
    >> > end sub
    >> >
    >> > Julian wrote:
    >> >>
    >> >> For example, If A1:K11 is selected, then the Array in VBA should be:
    >> >>
    >> >> A(1,1) = A1, A(1,2) = B1 A(1,3) = C1 ...
    >> >> A(2,1) = A2, A(2,2) = B2 A(2,3) = C2 ...
    >> >> ...
    >> >>
    >> >> Thanks!
    >> >
    >> > --
    >> >
    >> > Dave Peterson

    >
    > --
    >
    > Dave Peterson




  6. #6
    Dave Peterson
    Guest

    Re: How to transfer selected cells' values into an array in VBA?

    Do you want to pass the address of the range?

    Option Explicit
    Public Function Testing(myInput As String) As Variant
    Testing = ActiveSheet.Range(myInput).Value
    End Function
    'test it out
    Sub testme()
    Dim myOtherArr As Variant
    myOtherArr = Testing("A1:L7")
    End Sub

    But you can pass it a range variable, too.

    Option Explicit
    Public Function Testing(myInput As Range) As Variant
    Testing = myInput.Value
    End Function
    'more testing
    Sub testme()
    Dim myOtherArr As Variant
    myOtherArr = Testing(ActiveSheet.Range("A1:L7"))
    End Sub

    I like the second version--I don't have to rely on any particular sheet being
    the activesheet--just be specific in the call:

    myOtherArr = testing(worksheets("sheet99").range("a1:x99"))
    or
    myOtherArr _
    =
    testing(workbooks("otherworkbook.xls").worksheets("sheet99").range("a1:x99"))


    Julian wrote:
    >
    > I mean can I create a Module like this:
    >
    > Public Function Testing(myInput As Variant)
    > Dim myArr As Variant
    > myArr = ActiveSheet.Range(myInput).Value
    > End Function
    >
    > Thanks again!
    >
    > "Dave Peterson" <[email protected]> wrote in message
    > news:[email protected]...
    > > myarr = selection.value
    > >
    > > I'm not sure what flexible means???
    > >
    > > Julian wrote:
    > >>
    > >> Thanks...
    > >> But what should I do if the selection is flexible?
    > >>
    > >> "Dave Peterson" <[email protected]> wrote in message
    > >> news:[email protected]...
    > >> > Option Explicit
    > >> > sub testme
    > >> > dim myArr as variant
    > >> > myArr = activesheet.range("a1:K11").value
    > >> > end sub
    > >> >
    > >> > Julian wrote:
    > >> >>
    > >> >> For example, If A1:K11 is selected, then the Array in VBA should be:
    > >> >>
    > >> >> A(1,1) = A1, A(1,2) = B1 A(1,3) = C1 ...
    > >> >> A(2,1) = A2, A(2,2) = B2 A(2,3) = C2 ...
    > >> >> ...
    > >> >>
    > >> >> Thanks!
    > >> >
    > >> > --
    > >> >
    > >> > Dave Peterson

    > >
    > > --
    > >
    > > Dave Peterson


    --

    Dave Peterson

  7. #7
    Julian
    Guest

    Re: How to transfer selected cells' values into an array in VBA?

    I know what to do now, thanks a lot!!


    "Dave Peterson" <[email protected]> wrote in message
    news:[email protected]...
    > Do you want to pass the address of the range?
    >
    > Option Explicit
    > Public Function Testing(myInput As String) As Variant
    > Testing = ActiveSheet.Range(myInput).Value
    > End Function
    > 'test it out
    > Sub testme()
    > Dim myOtherArr As Variant
    > myOtherArr = Testing("A1:L7")
    > End Sub
    >
    > But you can pass it a range variable, too.
    >
    > Option Explicit
    > Public Function Testing(myInput As Range) As Variant
    > Testing = myInput.Value
    > End Function
    > 'more testing
    > Sub testme()
    > Dim myOtherArr As Variant
    > myOtherArr = Testing(ActiveSheet.Range("A1:L7"))
    > End Sub
    >
    > I like the second version--I don't have to rely on any particular sheet
    > being
    > the activesheet--just be specific in the call:
    >
    > myOtherArr = testing(worksheets("sheet99").range("a1:x99"))
    > or
    > myOtherArr _
    > =
    > testing(workbooks("otherworkbook.xls").worksheets("sheet99").range("a1:x99"))
    >
    >
    > Julian wrote:
    >>
    >> I mean can I create a Module like this:
    >>
    >> Public Function Testing(myInput As Variant)
    >> Dim myArr As Variant
    >> myArr = ActiveSheet.Range(myInput).Value
    >> End Function
    >>
    >> Thanks again!
    >>
    >> "Dave Peterson" <[email protected]> wrote in message
    >> news:[email protected]...
    >> > myarr = selection.value
    >> >
    >> > I'm not sure what flexible means???
    >> >
    >> > Julian wrote:
    >> >>
    >> >> Thanks...
    >> >> But what should I do if the selection is flexible?
    >> >>
    >> >> "Dave Peterson" <[email protected]> wrote in message
    >> >> news:[email protected]...
    >> >> > Option Explicit
    >> >> > sub testme
    >> >> > dim myArr as variant
    >> >> > myArr = activesheet.range("a1:K11").value
    >> >> > end sub
    >> >> >
    >> >> > Julian wrote:
    >> >> >>
    >> >> >> For example, If A1:K11 is selected, then the Array in VBA should
    >> >> >> be:
    >> >> >>
    >> >> >> A(1,1) = A1, A(1,2) = B1 A(1,3) = C1 ...
    >> >> >> A(2,1) = A2, A(2,2) = B2 A(2,3) = C2 ...
    >> >> >> ...
    >> >> >>
    >> >> >> Thanks!
    >> >> >
    >> >> > --
    >> >> >
    >> >> > Dave Peterson
    >> >
    >> > --
    >> >
    >> > Dave Peterson

    >
    > --
    >
    > Dave Peterson




+ 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