display range

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jagguy
    New Member
    • Sep 2007
    • 23

    display range

    i can get a single value from a worksheet using vb.net and excel.

    q)How do i get a range of values eg A2 to A5 and print them out eg with a msgbox. I know i can use the cells command but i want a range command where specify excel cell range.

    'MyString = XL.Cells(4, 1).Value
    MyString = XL.Range("A2", "A5").Value 'error
    MsgBox(MyString )
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    I Guess, Return Range should be assigned to an Array:

    Try This :

    Dim MyArr() As String
    MyArr = XL.Range("A2:A5 ").Value

    And Loop through MyArr, to get all the Values..

    Regards
    Veena

    Comment

    • kadghar
      Recognized Expert Top Contributor
      • Apr 2007
      • 1302

      #3
      Originally posted by QVeen72
      Hi,

      I Guess, Return Range should be assigned to an Array:

      Try This :

      Dim MyArr() As String
      MyArr = XL.Range("A2:A5 ").Value

      And Loop through MyArr, to get all the Values..

      Regards
      Veena
      It wont work all the times for Strings array, try using a variant. The nice thing about asigning a Range to a Variant, is that the variant wont change into a Variant/Range, but into a Variant's array.
      so

      dim MyArr
      MyArr = Range("A2:A5")

      will make MyArr a 2 dimensioned array (1 to 4, 1 to 1) of variants, which index start in 1.

      HTH

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by kadghar
        dim MyArr
        MyArr = Range("A2:A5")

        will make MyArr a 2 dimensioned array (1 to 4, 1 to 1) of variants, which index start in 1.
        That's weird. It seems pretty silly of it to create a two-dimensional array, don't you think? Then again, computers do work in mysterious ways.

        Comment

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          Originally posted by Killer42
          That's weird. It seems pretty silly of it to create a two-dimensional array, don't you think? Then again, computers do work in mysterious ways.
          No, its quite useful when working with Excel's VBA, because you can use the same FOR for a range an its array. Also you can asign an 2 dimensioned array to a range, so it'll be writen in the worksheet. e.g.

          range("A3,A5") = Arr

          =) Where Arr is a 2 dimension array (1 to 3, 1 to 1)

          Comment

          • jagguy
            New Member
            • Sep 2007
            • 23

            #6
            I asked this before but i still dont know

            can i set a range of values from excel to a datagrid?

            Comment

            • kadghar
              Recognized Expert Top Contributor
              • Apr 2007
              • 1302

              #7
              Originally posted by jagguy
              I asked this before but i still dont know

              can i set a range of values from excel to a datagrid?
              can you asign an Array to a datagrid?

              Comment

              • jagguy
                New Member
                • Sep 2007
                • 23

                #8
                i can assign a 1D array to a datagrid yes.
                i cant assign a 2d array so i need to convert the 2d array to 1D array but i cant get rows and cols in a datagrid with rows and cols (just 1 col).

                This is very complicated!

                Comment

                • kadghar
                  Recognized Expert Top Contributor
                  • Apr 2007
                  • 1302

                  #9
                  Originally posted by jagguy
                  i can assign a 1D array to a datagrid yes.
                  i cant assign a 2d array so i need to convert the 2d array to 1D array but i cant get rows and cols in a datagrid with rows and cols (just 1 col).

                  This is very complicated!
                  [CODE=vb]dim a
                  dim b()
                  dim i as long
                  a = range("a2:a5")
                  redim b(1 to ubound(a))
                  for i = 1 to ubound(a)
                  b(i) = a(i,1)
                  next[/CODE]

                  And now b is your 1D array.

                  ^.^

                  Comment

                  • jagguy
                    New Member
                    • Sep 2007
                    • 23

                    #10
                    i have a range "A2:B5" but i can convert to array.

                    the problme is assigning to a datagrid

                    datagrid1.datas ource= myarray ' myarray is single array from 2D array

                    'the datagrid is just 1 col and i want rows and cols like the 2D array origanlly is.

                    it is messy

                    Comment

                    • kadghar
                      Recognized Expert Top Contributor
                      • Apr 2007
                      • 1302

                      #11
                      Originally posted by jagguy
                      i have a range "A2:B5" but i can convert to array.

                      the problme is assigning to a datagrid

                      datagrid1.datas ource= myarray ' myarray is single array from 2D array

                      'the datagrid is just 1 col and i want rows and cols like the 2D array origanlly is.

                      it is messy
                      in my last post, 'a' is a 2D array, and then 'b' is the 1D array that you may use.

                      Comment

                      • jagguy
                        New Member
                        • Sep 2007
                        • 23

                        #12
                        i have used a datatable which is more effective. the trick is to fill data from excel2d array and copy to datatable.

                        Comment

                        Working...