How to get Excel Range in Array

.

Many times while developing an application in Excel VBA, we need all the values stored in an excel Cell Range in an Array variable. So that at any point in time we can access the value from the array, rather than going to Excel Cell and reading it from there. Also, now that you have all the values from a cell range in an array, it is very easy to iterate them and perform any kind of action we want on them.

VBA Code to store cell range values in an Array variable

This is one of the functions I always keep handy. I am sharing it with you guys so that you don’t have to write such functions every time you are working on any excel VBA project. The following function will accept Cell Range as Input and Return a String Array having all the values in that cell Range.

Step 1. Copy and Paste the below code in Excel Macro Module



Public Function GetArray(xlRange As Range) As String()
    Dim strArray() As String
    Dim iCounter As Integer
    Dim intCount As Integer
    Dim xlCell As Range
    
    iCounter = 0
    intCount = xlRange.Cells.Count
    
        ReDim strArray(0 To intCount - 1)
        For Each xlCell In xlRange
                strArray(iCounter) = xlCell.Value
                iCounter = iCounter + 1
        Next
    
    GetArray = strArray
 
End Function

Step 2. You can use the above Function anywhere in the Excel macro and it will return the String Array.


Sub GetArray()
    Dim strArr() As String
    strArr() = GetArray(ActiveSheet.Range("A1:A5"))
End Sub

I am storing All the values in a Single Dimensional Array. Therefore If you are passing Range of Single Row or a Single Column then all the Values will be Stored as a single array.

For Example: 1. If the Range is “A1:A5” and Array is strArr then Array will be defined as below:

Cell Range to Array

Excel Cell Range to Array – One Dimensional Array

For Example: 2. If the Range is “A1:B5” and Array is strArr then Array will be defined as below:

Range to Array

Excel Range to Array – Single dimensional array

Conclusion

No matter how many rows and columns are part of the input range you provide, array returned is single dimensional array. This statement in the above function For Each xlCell In xlRange traverse by rows. In other words, first it returns all the column values from left to right for the FIRST ROW then SECOND ROW and so on. This is also very clear from the above pictures.
Note: It is also possible to store them in 2 Dimensional Array, if you want. You need to define a two Dimensional array to store it.

If you want the workbook with this Function Mail Me

To Check out more Excel Macro Tutorials, visit Excel Macro Tutorial

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

How to Copy content from Word using VBA

As many of us want to deal with Microsoft Word Document from Excel Macro/VBA. I am going to write few articles about Word from Excel Macro. This is the first article which opens a Word Document and read the whole content of that Word Document and put it in the Active...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

7 Comments

  1. prabhat

    Can we sort Excel Range in Array without writing in excel or pickup one element of array based on the criteria

    Reply
    • Vishwamitra Mishra

      Hi Prabhat,
      We can sort data stored in an Array without writing it in Excel.
      Also you can pick up any element of the array by passing the reference number. Kindly Confirm, If it helps.
      Thanks,
      Vishwa

      Reply
  2. prabhat

    i want to know how both can be done

    Reply
    • Vishwamitra Mishra

      Give me your sample requirement and Excel, i will send you the code

      Reply
  3. ajay

    I have CSV file sent to me incorrectly

    Cell Column should be Day that is numeric and I need converted Need to convert

    If

    0 = Monday

    1=Tuesday

    2=Wednesday

    3=Thursday

    4= Friday

    5= Saturday

    6 = Sunday

    Reply
  4. ramon

    hi, when i select Case ele.classname and im looping through external link to retrieve text elements likes this

    —————————–macro1——–

    Sub test()

    Dim eRow As Long

    Dim ele As Object

    Set sht = Sheets("Sheet1")

    RowCount = 1

    sht.Range("A" & RowCount) = "left"

    eRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

    Set objIE = CreateObject("internetexplorer.application")

    With objIE

    .Visible = True

    .navigate "http://external-website-link.com"

    Do While .Busy Or _

    .readyState 4

    DoEvents

    Loop

    Do While .Busy Or _

    .readyState 4

    DoEvents

    Loop

    For Each ele In .document.all

    Select Case ele.classname

    Case "Here is the website HTML-MAIN-CLASS"

    RowCount = RowCount + 1

    Case "Here is the website HTML-SUB-CLASS"

    sht.Range("a" & RowCount) = ele.innertext

    —————————————–

    my question that its working for text elements, but how can i specify a link elements or picture elements to loop and extract them into my rows.

    How to get image elements or image links ?

    Reply
  5. Vinay

    the above code is not working and its give Run time error 13 & description like Type Mismacth…can you help me for this

    Reply

Trackbacks/Pingbacks

  1. Learn Excel Macro Spreading an Array values across Rows in Excel - VBA - [...] know more about Range and Array distribution in detail, you can read this article. Comments commentsRelated [...]
  2. Learn Excel Macro Excel Macro Beginners - What is Range Object in Excel VBA - [...] But if your Range has more than one cell then it is stored in an Array format and Range(“A1:G5″).Value…
  3. Spreading an Array values across Rows in Excel - VBA - Let's excel in Excel - […] To know more about Range and Array distribution in detail, you can read this article. […]
  4. Excel Macro Beginners - What is Range Object in Excel VBA - Let's excel in Excel - […] But if your Range has more than one cell then it is stored in an Array format and Range(“A1:G5”).Value…

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest