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

Entering a Page Break from the Keyboard

Need to force Word to move text to the top of the next page? It's easy when you use the keyboard shortcut for a page break.

Discover More

Days Left in the Year

Sometimes it is handy to know how many days are left in the current year. This tip provides a quick formula that ...

Discover More

Making a Longer Quick Access Toolbar

The Quick Access Toolbar is where you put all the tools to which you need quick access. (Makes sense, huh?) There are ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Hiding Macros

Need to hide some macros in your workbook? There are three ways you can do it, as covered in this discussion.

Discover More

Checking for the Existence of a File

The data stored in a worksheet can often correspond to information external to that worksheet. For instance, you might ...

Discover More

Getting Rid of the "Enable Macros" Notice

Do you get tired of the dialog box that says "do you want to enable macros" that is displayed when you open a workbook. ...

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 six minus 2?

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.