+ Reply to Thread
Results 1 to 10 of 10

How to put range into redimed array?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    How to put range into redimed array?

    Hi,

    i have code like here:

    Sub test()
    
      Dim a() As String
      
      ReDim a(1 To 6, 1 To 5)
      
      Dim rng As Range
      
      Set rng = ActiveSheet.Range("A2:E7")
      
      a = rng
    
    End Sub
    which throws an error about type.

    It is possible to assign to string array range?
    Or i have to loop?

    the purpose to have array as string it to not loose leading zeros while outputting variant array.

    Thanks!,
    Best,
    Jacek
    Attached Files Attached Files

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: How to put range into redimed array?

    You have to loop.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: How to put range into redimed array?


    Hi,

    as it has no sense to allocate the ranges values to a dimed array - like allocating an useless Range variable as well ! - but
    if you really need it then yes just use a loop …

  4. #4
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: How to put range into redimed array?

    Quote Originally Posted by jaryszek View Post
    ... It is possible to assign to string array range? ...
    Hello. Two equivalents to what you've tried but didn't work for you would be:

    Sub test1()
    Dim a
    Dim rng As Range
    
    Set rng = ActiveSheet.Range("A2:E7")
    a = rng
    End Sub
    
    Sub test2()
    Dim a
    a = ActiveSheet.Range("A2:E7")
    End Sub
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: How to put range into redimed array?

    Neither of those approaches will give you a String array though.

  6. #6
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: How to put range into redimed array?

    run your 'test' macro in attached file.

    Sub test()
    Dim a() As Variant
    a = Sheet1.Range("A2:E8").Value2
    [H2] = a(1, 1): [I2] = a(6, 5): [J2] = a(7, 5)
    End Sub
    Attached Files Attached Files
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: How to put range into redimed array?

    Quote Originally Posted by jaryszek View Post
    Hi,
    the purpose to have array as string it to not loose leading zeros while outputting variant array.
    Sub test()
        Dim a
        With [a1:e7]
            a = Evaluate("if(left(" & .Address & ",1)=""0"",""'"","""")&" & .Address)
            [g1].Resize(.Rows.Count, .Columns.Count) = a
        End With
    End Sub
    Last edited by jindon; 11-28-2022 at 10:32 PM.

  8. #8
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: How to put range into redimed array?

    Wow thank you Guys!!

    Awesome i am taking your ideas to my project!

    Best,
    Jacek

  9. #9
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: How to put range into redimed array?

    Quote Originally Posted by jaryszek View Post
    Wow thank you Guys!! Awesome i am taking your ideas to my project!
    Best, Jacek
    Thanks for the +rep.

  10. #10
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: How to put range into redimed array?

    @jaryszek, thanks for the feedback and added rep point.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA Evaluate Array then imput into range adjusting forumla in Array down with the Range
    By fireguy7 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-11-2019, 03:40 PM
  2. Countifs on an Array Range using multiple criteria in an Array
    By Mysore in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-12-2016, 04:39 PM
  3. [SOLVED] Help with: Range Array SelectCase Loop delete: Array All Sheet.Names
    By dlow in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-08-2015, 07:11 PM
  4. Can I make the row lookup array/range part in an array formula variable?
    By OLLY-7 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-18-2014, 09:06 AM
  5. [SOLVED] Populate one array from another array and print new array as a range
    By Kaden265 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2014, 07:52 AM
  6. [SOLVED] Add Range to Array find Array value and Paste back to Range
    By Foreverlearning in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-28-2012, 05:45 PM
  7. Does filling part of an array from a range re-dimension the array?
    By barryleajo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-04-2011, 10:09 AM

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