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

Maintaining the Active Cell

Move from one worksheet to another, and Excel selects whatever cell was last used in the worksheet you are selecting. If ...

Discover More

Hyperlinks in Protected Documents

Need the ability to follow a hyperlink in a document you've protected? If so, you'll need to examine different ways of ...

Discover More

Deleting Index Entries

When you construct an index you need to insert all sorts of index fields throughout your document. If you want to later ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Calculating the Distance between the Top of the Window and Row 1

Normally Excel positions a UserForm in the center of your screen. You may want to position the form elsewhere, more ...

Discover More

Replacing Commas with Periods

Want to replace all commas in a formatted number with periods, and vice-versa? There are a couple of approaches you can ...

Discover More

Triggering an Event when a Worksheet is Deactivated

One way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's ...

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 9 + 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.