Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Filling a Range of Cells with Values.

Filling a Range of Cells with Values

Written by Allen Wyatt (last updated July 16, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


8

Jonathan is creating a macro and needs to fill a range of cells with values. For instance, if he needs to fill the range A1:C1, it currently takes three statements to fill that range:

Range("A1") = "Test1"
Range("B1") = "Test2"
Range("C1") = "Test3"

He wonders if there is a way to fill them in a single statement, similar to the following:

Range("A1:C1") = ("Test1","Test2","Test3")

Jonathan's desired syntax is close, but it won't work. Here's how it will work:

Range("A1:C1") = Array("Test1","Test2","Test3")

Note the use of the Array statement, which tells VBA that what follows should be considered a sequence of values to be used in the sequence of cells at the left of the operator. Interestingly enough, you could stuff values into variables and also use the Array statement, as shown here:

sOne = "Apples"
sTwo = "Oranges"
sThree = "Artichokes"
Range("A1:C1") = Array(sOne, sTwo, sThree)

You can also work with straight variables, if you prefer:

Dim sMyStrings(2) As String
sMyStrings(0) = "Apples"
sMyStrings(1) = "Oranges"
sMyStrings(2) = "Artichokes"
Range("A1:C1") = sMyStrings

The above code could also be rewritten, as follows:

Dim sMyStrings(2) As String
sMyStrings = Array("Apples", "Oranges", "Artichokes")
Range("A1:C1") = sMyStrings

Finally, if you wanted to have the values placed into a single column rather than in a row, you would need to use the Transpose function, in this manner:

Range("A1:A3") = Application.Transpose(Array("Test1","Test2","Test3"))

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11702) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Filling a Range of Cells with Values.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Erasing Table Lines

When creating tables, Word provides a handy tool that you can use. Once the table is in place, you can use the table ...

Discover More

Printing Very Large Paper Sizes

Need to print on large pieces of paper? Word has a limit on the size of the paper it can use, but that might not be the ...

Discover More

Dealing with Circular References

Circular references occur when a formula includes a reference to the cell in which the formula appears. Here's how you ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Creating a Function Inventory for a Workbook

Your worksheets are very often made up of formulas and these formulas are made up of functions. If you ever want to ...

Discover More

Stepping Through a Non-Contiguous Range of Cells

Using macros to step through each cell in a selection is a common occurrence. What if that selected range is made up of ...

Discover More

Replacing Some Formulas with the Formula Results

Macros are often used to process the data stored in a worksheet. Some of these processing needs can be pretty specific to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2022-08-02 11:49:05

Mike D.

@J.Woolley

Ahhh, a little more has crept into my gray matter.

Allen's code had the issue, not me. I changed the DIM to Variant and deleted the (2) from the length and it worked perfectly.

Thank you
--------------------------------------------------------------------------------------------
Sub mike_2()

Dim sMyStrings() As Variant
sMyStrings = Array("Apples", "Oranges", "Artichokes")
Range("A1:C1") = sMyStrings

End Sub
--------------------------------------------------------------------------------------------


2022-08-02 10:28:34

J. Woolley

@Mike D.
The VBA Array(...) function must be assigned to a Variant variable or Variant array of undeclared size, so this is not allowed for two reasons:
    Dim A(2) as String
    A = Array("Apples", "Oranges", "Artichokes")
Note that Variant is the default type, so these are OK:
    Dim A() As Variant, B As Variant, C, D()
    A = Array("Apples", "Oranges", "Artichokes")
    B = Array(1, 2, 3)
    C = Array("Apples", "Oranges", "Artichokes")
    D = Array(1, 2, 3)
Also, be careful with Option Base; the default is 0, but it might have been declared as 1 in your module. The qualified function VBA.Array(...) is not affected by Option Base, so this always creates an array like A(0 to 2):
    A = VBA.Array("Apples", "Oranges", "Artichokes")
But this might create A(0 to 2) or A(1 to 3) depending upon Option Base:
    A = Array("Apples", "Oranges", "Artichokes")
See https://sites.google.com/view/MyExcelToolbox/


2022-08-02 08:32:30

Mike D.

Thank you Peter and Willy.
I have been able to get it to work with the other code Allen posted, I was trying to figure out what I was doing wrong.
I experimented with the code and figured out a few things. I will try your suggestions for fun. Learn, learn learn.

What I am hoping is to figure out the syntax for the variable = Array(data, data, data).
I feel it should work but there is something simple I am missing.

Thanks again


2022-08-02 06:46:50

Willy Vanhaelen

@Peter Atherton

You can even write it as a one liner:

Sub t()
Range("A1:C1") = Split("Apples Oranges Artchokes")
End Sub


2022-08-02 06:18:24

Peter Atherton

Mike D

Try this

Sub t()
Dim s
Dim sMyStrings As Variant

s = "Apples, Oranges,Artichokes"
sMyStrings = Split(s, ",")
Range("A1:C1") = sMyStrings
End Sub


2022-08-01 15:17:25

Mike D.

I copied your code from above and ran into an error I cannot resolve.

Dim sMyStrings(2) As String
sMyStrings = Array("Apples", "Oranges", "Artichokes")
Range("A1:C1") = sMyStrings

Compile Error:
Can't assign to array

The editor highlights the sMyStrings variable as the culprit.
What am I missing? (Running 365)


2022-08-01 14:23:57

Mike D.

I copied your code from above and ran into an error I cannot resolve.

Dim sMyStrings(2) As String
sMyStrings = Array("Apples", "Oranges", "Artichokes")
Range("A1:C1") = sMyStrings

Compile Error:
Can't assign to array

The editor highlights the sMyStrings variable as the culprit.
What am I missing? (Running 365)


2022-07-16 10:14:03

J. Woolley

In newer versions of Excel with dynamic arrays, here is yet another way using curly brackets like {...}.
For 3 columns in one row:
Range("A1").Formula2 = "={""Test1"",""Test2"",""Test3""}"
For 3 rows in one column:
Range("A1").Formula2 = "={""Test1"";""Test2"";""Test3""}"
Each results in an array formula of text constants instead of the Tip's array of constant text values.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.