Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. 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, 2021, and Excel in Microsoft 365


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, 2021, and Excel in Microsoft 365. 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

Generating Random Testing Data

Need to test your formulas? Then you need some testing data that you can use to see if the formulas function as you ...

Discover More

Protecting an Entire Workbook

Want to stop other people from making unauthorized changes to your workbook? Excel provides a way that you can protect ...

Discover More

Checking for Valid Hyperlinks

If you have a document containing a lot of hyperlinks, it would be cool if there was a way to check all those hyperlinks ...

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Running a Macro while in Edit Mode

Excel doesn't allow you to run a macro while editing the contents of a cell. The only solution is to get out of Edit ...

Discover More

Pulling First Letters from Parenthetical Text

When working with text in Excel, you can slice and dice it in many ways. This tip shows how to pull first letters from ...

Discover More

Removing a Directory

Macros allow you to perform all sorts of file-related operations. One such operation allows you to delete a directory. ...

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 5 - 3?

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.